We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
awk是一个强大的文本分析工具
相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。 awk有3个不同版本: awk、nawk和gawk,未作特别说明,一般指gawk,gawk 是 AWK 的 GNU 版本。
awk '{pattern + action}' {filenames}
awk '{print $n}' xxx
grep "bash" /etc/passwd | awk -F ':' '{print $1,$7}'
'
grep "bash" /etc/passwd | awk -F '\047' '{print $1,$7}'
\047
\x27
字符串
xxxx | awk -F 'took' '{print $1}'
grep "bash" /etc/passwd | awk -F ':' '{print $NF}'
grep "bash" /etc/passwd | awk -F ':' '{print $(NF-1)}'
awk '/(root|net|ucp)/' /etc/passwd
cat inf_log.txt |awk -F ' ' '{print $1,"|",$5,"|",$7}'
string
awk -v RS="@#$j" '{print gsub(/string/,"&")}' yourfile
shell
awk -v RS="@#$j" '{print gsub(/'$text_id'/,"&")}' yourfile
awk 'BEGIN{printf "%.3f\n", '$tcnt'/'$totalcnt'}'
awk '$5==string {print $0}' yourfile
awk '$5==string && $6==string2 {print $0}' yourfile
awk -F '[/_ ]' '$2!=teid {print $6}' teid="$text_id"
-F '[/_ ]'
/
_
awk -F '[| ]+'
+
awk的sub/gsub函数用来替换字符串
AAAA
awk '{ sub(/AAAA/,"BBBB"); print $0 }' t.txt
awk '{ gsub(/AAAA/,"BBBB"); print $0 }' t.txt
awk '/CCCC/ { gsub(/AAAA/,"BBBB"); print $0; next } { print $0 }' t.txt
CCCC
awk '{ sub(/\<AAAA\>/,"BBBB"); print $0 }' t.txt
awk '{ gsub(/^A*/,"B"); print $0 }' t.txt
ls */*|awk -F '/' '{print "mv "$1"/"$2" "$1"_"$2}'|bash
将文件夹 $1 下的 $2 移到当前目录,并重命名为 $1_$2 的格式
$1
$2
$1_$2
打印使用 / 分割后除第一个和最后一个之外的字符串
awk -F '/' '{for(i=2;i<NF;++i) printf $i"/";printf "\n"}'
The text was updated successfully, but these errors were encountered:
No branches or pull requests
awk
awk是一个强大的文本分析工具
相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。
awk有3个不同版本: awk、nawk和gawk,未作特别说明,一般指gawk,gawk 是 AWK 的 GNU 版本。
UseCase
awk '{print $n}' xxx
grep "bash" /etc/passwd | awk -F ':' '{print $1,$7}'
'
grep "bash" /etc/passwd | awk -F '\047' '{print $1,$7}'
-
\047
十进制'
-
\x27
十六进制'
字符串
xxxx | awk -F 'took' '{print $1}'
grep "bash" /etc/passwd | awk -F ':' '{print $NF}'
grep "bash" /etc/passwd | awk -F ':' '{print $(NF-1)}'
awk '/(root|net|ucp)/' /etc/passwd
cat inf_log.txt |awk -F ' ' '{print $1,"|",$5,"|",$7}'
string
在文件中出现的次数awk -v RS="@#$j" '{print gsub(/string/,"&")}' yourfile
shell
查询某变量在 文件中出现的次数awk -v RS="@#$j" '{print gsub(/'$text_id'/,"&")}' yourfile
awk 'BEGIN{printf "%.3f\n", '$tcnt'/'$totalcnt'}'
awk '$5==string {print $0}' yourfile
awk '$5==string && $6==string2 {print $0}' yourfile
awk -F '[/_ ]' '$2!=teid {print $6}' teid="$text_id"
-
-F '[/_ ]'
使用多个分隔符/
_
和-
awk -F '[| ]+'
使用+
将连续出现的分隔符当做一个来处理替换字符
AAAA
awk '{ sub(/AAAA/,"BBBB"); print $0 }' t.txt
AAAA
awk '{ gsub(/AAAA/,"BBBB"); print $0 }' t.txt
AAAA
awk '/CCCC/ { gsub(/AAAA/,"BBBB"); print $0; next } { print $0 }' t.txt
- 替换出现
CCCC
的行中的AAAA
AAAA
&CCCC
awk '{ sub(/\<AAAA\>/,"BBBB"); print $0 }' t.txt
awk '{ gsub(/^A*/,"B"); print $0 }' t.txt
- 以A开头的字符串
使用bash 执行拼接的命令
打印除特定位置外的字符串
打印使用
/
分割后除第一个和最后一个之外的字符串Reference
The text was updated successfully, but these errors were encountered: