Perl,awk,sed One-Liners Explained, Part II: Text Conversion and Substitution

所有字符大写

1
perl -nle 'print uc' test.txt

所有字符小写

1
perl -nle 'print lc' test.txt

行首字母大写

1
2
3
perl -nle 'print ucfirst lc' test.txt
等同于
perl -nle 'print "\u\L$_"' test.txt

去掉每行行首空格

1
2
3
4
5
perl -ple 's^[ \t]+//' test.txt
awk '{ sub(/^[ \t]+/, ""); print }' test.txt
sed 's/^[ \t]*//' test.txt
等同于
perl- ple 's/^\s+//' test.txt

去掉从开头到结尾的空格

1
2
3
4
perl -ple 's/^[ \t]+|[ \t]+$//g' test.txt
awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }' test.txt
awk '{$1=$1; print}' test.txt
sed 's/^[ \t]*//;s/[ \t]*$//' test.txt

sub和gsub区别:sub替换遇到的第一个字符,而gsub相当于全局替换;

转换DOS/Windows换行符为UNIX换行符

1
2
3
4
perl -pe 's|\r\n|\n|' test.txt
awk '{ sub(/\r$/,""); print }' test.txt
sed 's/.$//' test.txt
sed 's/^M$//' test.txt

替换A为S

1
perl -pe 's/A/S/g' test.txt

仅替换最后一个A为S

1
sed 's/\(.*\)A/\1S/' test.txt

在C行替换A为S

1
2
3
perl -pe '/C/ && s/A/S/g' test.txt
awk '/C/ { gsub(/A/, "S") }; { print }' test.txt
sed '/C/s/A/S/g' test.txt

awk 中sort排序

1
awk -F ":" '{print $1 | "sort"}' /etc/passwd

删除第二列

1
awk '{$2=""; print}' test.txt

每一列倒序输出

1
awk '{for (i=NF;i>0;i--) printf("%s ",$i); printf ("\n")}' test.txt

sed实现tac功能

1
sed '1!G;h;$!d' test.txt

参数解释:

1!G表示第一行不执行G命令;
$!d表示最后一行不执行d命令;

tiramisutes wechat
欢迎关注