打包list中的文件
[root@test shell]# cat list
/etc/passwd
/etc/fstab
cat list | xargs -i tar rvPf list.tar {}
[root@test shell]# tar tvf list.tar
-rw-r--r-- root/root 2296 2010-09-29 18:21:04 /etc/passwd
-rw-r--r-- root/root 534 2010-05-13 23:40:39 /etc/fstab
替换2个或2个以上的空格和tab键
AssignmentType Caption Description
0 Windows Media Player Firefox Plugin Windows Media Player Firefox Plugin
0 ICBCSetupIntegration ICBCSetupIntegration
sed 's/ \{2,\}/|/g' 1
AssignmentType|Caption|Description|
0|Windows Media Player Firefox Plugin|Windows Media Player Firefox Plugin|
0|ICBCSetupIntegration|ICBCSetupIntegration|
awk 求列最大值
awk '{if(max<$1){max=$1}}END{print max}' 2
将[aabb]到name=1 之间的行注释掉
sed '/\[aabb\]/,/name=1/s/^/#/g' 1
将行倒序
echo 54321 | rev
12345
cat 1
2008/09/21 18:02:25
Error
Failed to opens the file!
Access is denied.
2008/09/22 18:02:25
Error
can not connect to the database !
Access is denied.
paste -sd '|||\n' 1 #paste -s 将多行合并成一行 -d 按循序指定每行的分割符
2008/09/21 18:02:25|Error|Failed to opens the file!|Access is denied.
2008/09/22 18:02:25|Error|can not connect to the database !|Access is denied.
[root@test shell]# cat 1
192.168.9.148 18 17561
192.168.9.212 14 2690
192.168.9.212 392 52187
192.168.9.148 3 398
192.168.9.148 2 768
[root@test shell]# awk '{S[$1]=S[$1]+$3}END{for(a in S) print a, S[a]}' 1
192.168.9.148 18727
192.168.9.212 54877
没五行生成一个文件
[root@test shell]# cat 1
1 aa
2 bbb
3 cccc
4 ddd
5 eee
6 777
7 iiiiiii
8 999
9 ddd
10 eee
11 aaa
12 bbb
13 ddd
split -l 5
awk '{n=sprintf("%02d",int((NR-1)/5));print >n}' 1
[root@test shell]# cat 1
00123456
00000389
[root@test shell]# cat 1 | cut -c 3-8 | sed 's/../&\//g'
12/34/56/
00/03/89/
awk 查找匹配到的行和它的下一行
cat 1
Time: 100722 19:09:58
# User@Host: @ b [192.168.10.1]
# Query_time: 2 Lock_time: 0 Rows_sent: 25 Rows_examined: 5272941
select 。。。。25 asc;
# Time: 100722 19:27:00
# User@Host: n] @ a[192.168.10.2]
# Query_time: 22 Lock_time: 0 Rows_sent: 3 Rows_examined: 3
select 。。。。3 asc;# Time: 100722 19:59:22
# User@Host: bain] @ b[192.168.10.2]
# Query_time: 2 Lock_time: 0 Rows_sent: 54 Rows_examined: 5284118
select 。。。。54 asc;;
# Time: 100722 19:59:54
# User@Host: in] @ a[192.168.10.2]
# Query_time: 2 Lock_time: 0 Rows_sent: 5 Rows_examined: 5284128
select 。。。。5 asc;;
# Time: 100722 20:00:42
awk '/Rows_sent/{if($7>5){print;getline;print}}' 1
# cat test.log
12 received, 15 packet loss, hello world
# cat test.log |sed -n 's/.*received,\(.*\)packet loss,.*/\1/p'
15
]# gawk '{a=$0;b=gensub(/.+received,(.+)packet loss,.+/,"\\1",a);print b}' test.log
15
12 received, 15 packet loss, hello world
# cat test.log |sed -n 's/.*received,\(.*\)packet loss,.*/\1/p'
15
]# gawk '{a=$0;b=gensub(/.+received,(.+)packet loss,.+/,"\\1",a);print b}' test.log
15
将同一客户号(第五列)的金额(第3列)相加,并插入到客户号列后。
cat 1
1,5416,350000,熊清喜,48260000140870,次级
2,5416,50000,杨小平,48260000142862,次级
3,5416,4500,杨明成,48260000144168,次级
4,5417,7000,蒲猛,48260000144266,次级
5,5418,10000,杨小平,48260000142862,可疑
6,5417,5000,张瑞东,48260000145752,可疑
7,5417,500000,杨明成,48260000144168,可疑
8,5419,200000,张瑞东,48260000145752,损失
9,5417,25000,杨小平,48260000142862,正常
10,5417,15000,蒲猛,48260000144266,正常
awk 'BEGIN{FS=OFS=","}NR==FNR{a[$5]+=$3;next}{if(FNR>1)
$NF=a[$5]","$NF}1' 1 1
11 15 18 2 5 6
12 14 3 6 7 9
2 13 16 17 18 19
[root@localhost shell]# awk '{m=0;n=0;for(i=1;i<=NF;i++)if($i>10)m++;else n++;p
rint NR": 大于10有"m"个 小于10有"n"个"}' 1
1: 大于10有3个 小于10有3个
2: 大于10有2个 小于10有4个
3: 大于10有5个 小于10有1个
4: 大于10有0个 小于10有0个
转载于:https://blog.51cto.com/jiaminqiang/439109