我想从文件中删除所有非ASCII字符.
我找到了一个带有tr的解决方案,但我想我需要在修改后写回该文件.
我需要以相对良好的性能来做到这一点.
有什么建议?
这个命令
echo "hello world" | awk '{split($0, array, " ")} END{print length(array) }'
Run Code Online (Sandbox Code Playgroud)
对我不起作用并给出此错误消息
awk:第1行:对数组数组的非法引用
为什么?
我试图\d在sed中使用正则表达式,但它不起作用:
sed -re 's/\d+//g'
Run Code Online (Sandbox Code Playgroud)
但这有效:
sed -re 's/[0-9]+//g'
Run Code Online (Sandbox Code Playgroud) 我有这种格式的日期:"2011年6月27日",我想将其转换为20110627
可以用bash做吗?
我有一个大文件中的数据(280列宽,700万行!)我需要交换前两列.我想我可以使用某种类型的awk for循环,打印$ 2,$ 1,然后一个范围到文件的末尾 - 但我不知道如何做范围部分,我不能打印$ 2 ,1美元,3美元...... 280美元!我在这里看到的大多数列交换答案都特定于具有可管理列数的小文件,所以我需要的东西不依赖于指定每个列号.
该文件是制表符分隔的:
Affy-id chr 0 pos NA06984 NA06985 NA06986 NA06989
Run Code Online (Sandbox Code Playgroud) 我有一个10 ^ 7行文件,其中我想从文件中随机选择1/100行.这是我所拥有的AWK代码,但它会预先包含所有文件内容.我的PC内存无法处理这样的问题.还有其他办法吗?
awk 'BEGIN{srand()}
!/^$/{ a[c++]=$0}
END {
for ( i=1;i<=c ;i++ ) {
num=int(rand() * c)
if ( a[num] ) {
print a[num]
delete a[num]
d++
}
if ( d == c/100 ) break
}
}' file
Run Code Online (Sandbox Code Playgroud) 我有一个函数输出许多行,我想在列中格式化.问题是任何特定的"单元格"(如果我可能使用该术语)的数据的宽度是可变的,所以将它管道化为像awk这样的东西并没有给我我想要的东西.
功能是"键"(不重要),我正在尝试这样的事情:
$ keys | awk '{ print $1"\t\t" $2 }'
Run Code Online (Sandbox Code Playgroud)
但是输出(它的一个片段,就是这样)看起来像这样:
"option-y" yank-pop
"option-z" execute-last-named-cmd
"option-|" vi-goto-column
"option-~" _bash_complete-word
"option-control-?" backward-kill-word
"control-_" undo
"control-?" backward-delete-char
Run Code Online (Sandbox Code Playgroud)
我怎么能强迫东西留在整洁的柱子里呢?这是可能的awk,还是我需要使用其他东西?
谁能告诉我如何使用awk打印包括零的行号?
这是我的输入文件stackfile2.txt
当我运行下面的awk命令时,我得到actual_output.txt
awk '{print NR,$0}' stackfile2.txt | tr " ", "," > actual_output.txt
Run Code Online (Sandbox Code Playgroud)
而我的预期输出是file.txt
如何打印以零(0)开头的行号?
如果我选择一个zip文件并右键单击"在此处提取",则会创建一个包含zip文件名的文件夹,并将zip文件的整个内容提取到其中.
但是,我想通过shell转换几个zip文件.但是,当我这样做
unzip filename.zip
Run Code Online (Sandbox Code Playgroud)
该文件夹"filename"未创建,但所有文件都被解压缩到当前目录中.
我查看了参数,但没有这样的参数.我也试过了
for zipfile in \*.zip; do mkdir $zipfile; unzip $zipfile -d $zipfile/; done
Run Code Online (Sandbox Code Playgroud)
但.zip必须使用sed删除2. $ zipfile和4. $ zipfile 的扩展名.如果我做
for zipfile in \*.zip; do mkdir sed 's/\.zip//i' $zipfile; unzip $zipfile -d sed 's/\.zip//i' $zipfile/; done
Run Code Online (Sandbox Code Playgroud)
它不起作用.
如何正确更换.zip扩展名$zipfile?
有没有比shell脚本更简单的方法?
我试图找到awk一个类的第二列数据的平均值.这是我当前的代码,我的讲师提供了框架:
#!/bin/awk
### This script currently prints the total number of rows processed.
### You must edit this script to print the average of the 2nd column
### instead of the number of rows.
# This block of code is executed for each line in the file
{
x=sum
read name
awk 'BEGIN{sum+=$2}'
# The script should NOT print out a value for each line
}
# The END block is processed after the last line is read …Run Code Online (Sandbox Code Playgroud)