1.
#1374848290
df -h
#1373845293
free -m
#1376945197
ifconfig
如何转换成
#2013-08-18 13:01:30
df -h
#2013-08-18 13:01:33
free -m
#2013-08-18 13:01:37
ifconfig
echo "123456789"|awk '{print substr($0,2,3)}'
234
echo "#123456789"|awk '/^#/{$0="#"substr($0,2,3)}1'
#123
awk '/^#/{$0="#"strftime("%F %T",substr($0,2))}1'
awk -F# 'NF>1{$0="#"strftime("%F %T",$2)}1'
sed '/^#/{s///;s/.*/date -d @& +"%F %T"/e;s/^/#/}'
sed '/^#/{s/#//g;s/.*/date -d @& +"%F %T"/e}'
2.
字符串为:/10.0.2.2/xxx_com_xzzz/ceshi01/abc
希望截取以/作为分割最后两个字段
得到 ceshi01/abc
echo "/10.0.2.2/xxx_com_xzzz/ceshi01/abc"|awk -F'/' '{print $(NF-1)"/"$NF}'
1 a b c
2 a c
3 a b c
4 a c
要求:将其余列相同的,将第一列求和后合并
得到如下结果:
4 a b c
6 a c
awk -F'a' '{a[$NF]+=$1}END{for(i in a){print a[i],"a"i}}' 1.txt
3.
http://bbs.chinaunix.net/thread-1853992-1-1.html 如何将竖排的数字横排
awk -F'>' '{if (/^[0-9]/){ORS=" ";print $0}else{ORS="\n";print "| "$2}}' f2.txt
awk '{printf /^[0-9].*/?$0" ":"| "substr($0,2)"\n"}' f2.txt
4.
http://bbs.chinaunix.net/thread-4096854-1-1.html 一段文本,求sed和awk截取方法 grep -Po '(?<=Address:)[^<]+'
或者awk -F '[^0-9.]+' '{print $2}'
或者awk -vRS='IP Address: ' 'NR>1{match($0,/[^<]+/,a);print a[0]}'
5.
在一个文件的倒数第三行
添加一个字母a
文件的行数是不确实的
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++){print a[i];if(i==NR-3)print "a"}}'
或者awk '{a[NR]=$0}END{for(i=1;i<=NR;i++){if(i==NR-3){print a[i]"\n$"}else{print a[i]}}}'
6.
http://bbs.chinaunix.net/thread-2288219-1-2.html awk实现grep -A3 -B3的效果,文本10G行
grep -B 5 "J"
7.
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
NancyMcNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
打印三个月捐献总额少于800 的人的姓名
awk -F':' '{ if( $(NF-2) +$(NF-1) +$NF < 800) print $1}'
8.
cat file
base_forge_meta
lib_gvg_scene
lib_fbt_room
格式改成:
base_forge_meta.beam,lib_gvg_scene.beam,lib_fbt_room.beam
awk -vOFS='' '{$2=".beam";s=s?s","$0:$0}END{print s}'
或者awk '{a[NR]=$0}END{for(i=1;i<=NR;i++){if(i<NR){printf a[i]".beam,"}else{printf a[i]".beam\n"}}}'
或者awk -v ORS="," '{a[NR]=$0}END{for(i=1;i<=NR;i++){if(NR-i>0){print a[i]".beam"}else{print a[i]".beam\n"}}}'
9.
把b文件包含a文件手机号码的行打印出来
awk 'NR==FNR{a[$1];next}{for(i in a)if($NF~i)print}' a b
(http://bbs.chinaunix.net/thread-520411-1-1.html
grep -f fileb filea
while read i
do
grep $i a
done < b)
cat a
123
345
567
cat b
abc
nba
dda 123
dad 345
dsf 456
ads 567
awk 'NR==FNR{a[$1];next}{for(i in a)if($NF~i)print}' a b
dda 123
dad 345
ads 567
awk 'NR==FNR{a[$1]}NR>FNR{for(i in a)if($NF~i)print}' a b
dda 123
dad 345
ads 567
awk 'NR==FNR{a[$1]}NR>FNR{if($NF in a)print}' a b
dda 123
dad 345
ads 567
awk 'NR==FNR{a[$1]}{for(i in a)if($NF~i)print}' a b
123
345
567
dda 123
dad 345
ads 567
10.
cat file
112312412
124312312452100000
4231433234
1241
412141
14233423423
212189
98079076
79978
根据文本中最长位数的那行进行补齐,不足位数的后面补0。
awk 'NR==FNR{if(max<length($0))max=length($0);next}{while(length($0)<max)$0=$0"0"}1' file file
awk '{a[NR]=$0;if(max<length($0))max=length($0)}END{for(i=1;i<=NR;i++){while(length(a[i])<max)a[i]=a[i]"0";print a[i]}}' file
11.
要求
日期 时间 游戏名称 服数
curl -s "http://www.91wan.com/" | sed -r '/flf_fwli.*(none)?/!d;s/<[^>]*>/ /g' | column -t
elinks -dump -no-numbering http://www.91wan.com | grep -EB3 -x '[ ]+[0-9]+服' | xargs -n4 | column -t
12.
打印不足三行
awk '{s=s""$0}!(NR%3){print s;s=""}END{if(s)print s}'
或者xargs -n3
13.
下面文本内容:
dp:11:79484:101 205
dp:1:79484:101 17
dp:4:79484:101 11
合并变成一行,最后一列累加
dp:79484:101 233
awk 'BEGIN{FS="[ :]+";SUBSEP=":"}{a[$1,$3,$4]+=$5}END{for(i in a)print i,a[i]}'
a[$1":"$3":"$4]+=$5 改成这个,就不用SUBSEP
或者awk -F'[: ]' -v OFS=":" '{total+=$5}END{print $1,$3,$4,total}'
awk 'BEGIN{FS="[ :]+"}{a[$1":"$3":"$4]+=$5}END{for(i in a){split(i,a2,SUBSEP);print a2[1],a2[2],a2[3],a[i]}}'
14.
echo 192_168_66_1 |awk '{gsub("_",".")}1'
var=192_168_66_1;echo ${var//_/.} 这种方式也能变量替换
15.
cat file
aaa
bbb
ccc
aaa
111
222
aaa
333
444
第二次匹配aaa时取后2行
awk '/aaa/{m++;x=NR}m==2&&NR-x<=2&&NR-x>0'
或者awk '/aaa/{s++}s==2{for(i=1;i<=2;i++){getline;print}}'
(awk '/aaa/{s++}s==2{for(i=1;i<=2;i++){getline s;prints}}'
awk '/aaa/{s++}1&&s==2{for(i=1;i<=2;i++){getline;print}}'
awk '/aaa/{s++};s==2{for(i=1;i<=2;i++){getline;print}}'
awk '/aaa/{s++}{if(s==2){{for(i=1;i<=2;i++){getline;print}}}}'
)
(cat file
111
222
333
444
匹配222及下一行
awk '/222/{print $0;getline s;print s}'
222
333
awk '/222/{a=2}a--'
222
333
每隔两行在行尾回车并插入一个$符号
awk '{print;getline;print;print "$"}'
awk '{print;getline s;print s"\n$"}'
awk '1;(NR+1)%2{print "$"}'
awk 'NR%2{print $0};(NR+1)%2{print $0"\n$"}'
awk '{print $0}(NR+1)%2{print "$"}'
awk '!(NR%2){print $0"\n$";next}1'
awk '{if(NR%2==0){print $0"\n$"}else{print $0}}'
awk '{if(NR%2==0){print gensub(/.*/,"&\n$","1")}else{print}}'
)
awk '/aaa/{m++;x=NR}m==2&&NR-x<=2' d
aaa
111
222
awk '/aaa/{m++;x=NR;print x}' d
1
4
7
awk '/aaa/{m++;x=NR}{print x,NR}' d
1 1
1 2
1 3
4 4
4 5
4 6
7 7
7 8
7 9
awk '/aaa/{m++;x=NR}m==2' d
aaa
111
222
awk '/aaa/{m++;x=NR}{if(m==2)print $0}' d
aaa
111
222
awk '/aaa/{m++}{if(m==2)print $0}{for(i=1;i<=2;i++){getline;print $0}}' d
bbb
ccc
aaa
111
222
333
444
awk '/aaa/{m++}{if(m==2);print $0}{for(i=1;i<=2;i++){getline;print $0}}' d
aaa
bbb
ccc
aaa
111
222
aaa
333
444
awk '/aaa/{m++}{if(m==2){for(i=1;i<=2;i++){getline;print $0}}}' d
111
222
awk '/aaa/{m++;x=NR}m==2&&NR-x<=2&&NR-x>0' d
111
222
awk '/aaa/{m++;x=NR}m==2&&NR-x>0' d
111
222
awk '{for(i=1;i<=2;i++){getline;print $0}}' d
bbb
ccc
111
222
333
444
cat file
video
10.128.12.13
gamesvr
10.128.12.15
log
10.150.13.10
awk '{a[NR]=$0}/[0-9]/{next}{for(i=1;i<=NR;i++)print i,$0}' file
1 video
1 gamesvr
2 gamesvr
3 gamesvr
1 log
2 log
3 log
4 log
5 log
awk '{a[NR]=$0}/[0-9]/{next}{for(i in a){print i,$0}}' file
1 video
1 gamesvr
2 gamesvr
3 gamesvr
4 log
5 log
1 log
2 log
3 log
awk '{a[NR]=$0}/[0-9]/{next}{for(i in a){print i,a[1],a[3],a[5]}}' file
1 video
5 video gamesvr
1 video gamesvr
2 video gamesvr
3 video gamesvr
4 video gamesvr log
5 video gamesvr log
1 video gamesvr log
2 video gamesvr log
3 video gamesvr log
16.
使用nmap扫描后得到的ip-mac片断如下:
Nmap scan report for 192.168.0.80
Host is up (0.00016s latency).
MAC Address: 60:A4:4C:A7:6C:51 (Unknown)
Nmap scan report for 192.168.0.84
Host is up (0.0084s latency).
MAC Address: 74:2F:68:BE:64:BF (Unknown)
Nmap scan report for 192.168.0.96
Host is up (0.00036s latency).
MAC Address: 60:A4:4C:A7:6C:9B (Unknown)
Nmap scan report for 192.168.0.97
Host is up (0.00036s latency).
MAC Address: 00:E0:66:75:A8:B1 (ProMax Systems)
怎样从这个文本文件中得到ip和对应的mac呢?
awk '/report/{printf $NF;printf " "}/MAC/{print $3}' 2.txt
17.
cat data.txt如下内容
video
10.128.12.13
gamesvr
10.128.12.15
log
10.150.13.10
10.150.13.22
要实现样式结果如下
10.128.12.13 #video
10.28.12.15 #gamesvr
10.150.13.10 #log
10.150.13.22 #log
awk '/^[a-z]/{a="#"$0;next}{print $0,a}' file
或者awk '/[a-z]/{m="#"$0}/[0-9]/{print $0,m}' file
awk '/[a-z]/{print $0}' file
video
gamesvr
log
awk '/[0-9]/{next}{print $0}' file
video
gamesvr
log
18.
有这样一个文件。
<version>v20130909195559<Version>
要求 20130909195559 对文本进行一次操作后数字自动加1
例如 结果是:<version>v20130909195560<Version>
echo '<version>v20130909195559<Version>' | awk 'BEGIN{RS="[0-9]+";ORS=OFS=""}{print $0,RT?RT+1:RT}'
或者awk 'BEGIN{ORS="";RS="[0-9]+"}NR==1{print $0(RT+1)}NR>1{print $0}'
19.
比如:
第一个文件:
10.124.0.1 000c.2980.001e
10.124.0.6 000c.2980.0561
第二个文件:
000c.2980.001e GigabitEthernet1/18
000c.2980.0561 Port-channel1
要变成:
10.124.0.1 000c.2980.001e GigabitEthernet
1/18
10.124.0.6 000c.2980.0561 Port-channel1
awk 'NR==FNR{a[$2]=$0}NR>FNR{print a[$1],$2}' a.txt b.txt
10.124.0.1 000c.2980.001e GigabitEthernet1/18
10.124.0.6 000c.2980.0561 Port-channel1
20.
echo {1..100}|awk '{for(;++i<=100;)j+=i;print j}'
echo {1..100}|awk '{for(i=1;i<=100;i++)j+=i;print j}'
echo {1..5}&& echo {1..4}&& echo {1..3} && echo {1..2}&& echo 1
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
awk 'BEGIN{for(i=5;i>=1;i--){for(j=1;j<=i;j++)printf j" ";print " " }}'
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
awk 'BEGIN{for(i=5;i>=1;i--)printf i" ";print " "}'
5 4 3 2 1
awk 'BEGIN{i=5;for(j=1;j<=i;j++)printf j" ";print " "}'
1 2 3 4 5
21.
http://bbs.chinaunix.net/thread-4111833-1-1.html
cat file
Douglas Bates
John Chambers
Peter Dalgaard
Seth Falcon
Robert Gentleman
Kurt Hornik
Stefano Iacus
Ross Ihaka
Friedrich Leisch
Uwe Ligges
Thomas Lumley
Martin Maechler
Duncan Murdoch
Paul Murrell
Martyn Plummer
Brian Ripley
Deepayan Sarkar
Duncan Temple Lang
Luke Tierney
pr -t -l10 -2 file
paste <(awk 'i=!i' file) <(awk '!(i=!i)' file)
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++) print a[i],a[++i]}' file
awk '{a[NR%10]=1}END{for(i=1;i<=NR;i++)print i,NR,a[i],a[i%10]}' file
1 20 1 1
2 20 1 1
3 20 1 1
4 20 1 1
5 20 1 1
6 20 1 1
7 20 1 1
8 20 1 1
9 20 1 1
10 20 1
11 20 1
12 20 1
13 20 1
14 20 1
15 20 1
16 20 1
17 20 1
18 20 1
19 20 1
20 20 1
cat file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
awk '{b=(a[NR%10]=a[NR%10]?a[NR%10]"\t"$0:$0);print b}' file
1
2
3
4
5
6
7
8
9
10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
awk '{a[NR%10]=a[NR%10]?a[NR%10]"\t"$0:$0;print a[NR%10]}' file
1
2
3
4
5
6
7
8
9
10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
awk '{a[NR%10]=a[NR%10]?3"\t"$0:$0;print a[NR]}' d
1
2
3
4
5
6
7
8
9
awk '{a[NR%10]=a[NR%10]?3"\t"$0:$0;print a[1]}' d
1
1
1
1
1
1
1
1
1
1
3 11
3 11
3 11
3 11
3 11
3 11
3 11
3 11
3 11
3 11
awk '{a[NR%10]=a[NR%10]?a[NR%10]"\t"$0:$0;print a[2]}' file
2
2
2
2
2
2
2
2
2
2
2 12
2 12
2 12
2 12
2 12
2 12
2 12
2 12
2 12
22.
cat file
001784 HCART3 ACS 3 - - 15 91488 ACTIVE
001812 HCART3 ACS 3 - - 15 94912 ACTIVE
fwefwrefwerfer
001812 HCART3 ACS 3 - - 15 94912 ACTIVE
fwefwefwe
001812 HCART3 ACS 3 - - 15 94912 ACT
统计ACTIVE行出现的次数:
awk '/ACTIVE/{p++}END{print p}'
23.
cat file
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
pattern等于ddd时,取其前1行:
awk '!/ddd/{m=$0}/ddd/{print m}' file
awk '!/ddd/{p=NR;a[NR]=$0}/ddd/{print a[p];exit}'
awk '{a[NR]=$0}/eee/{p=NR}END{print a[p-1]}'
24.
cat file
1 a
2 b
3 c
close关闭文件
awk '{print NR,$0 > "b.txt";close("b.txt")}' file
3 c
每次写入后都关闭文件,那么下次写入时就又重新打开文件
而每次打开又是覆盖的方式,这样file里最后就只剩下最后一行记录
25.
有没有将两个输出或者说变量上每列分别合在一起的方法
http://bbs3.chinaunix.net/viewthread.php?tid=1752341
$ var1='a
> b
> c
> d'
$ var2='A
> B
> C
> D'
$ export var2
$ echo "$var1" | awk '{"echo \042$var2\042"|getline var2;print $1""var2}'
aA
bB
cC
dD
26.
从shell中向awk传入字符串
echo "Stand-by" | awk '{print length($0)}'
设置文件名为一变量,管道输出到awk,只返回其扩展名
STR="mydoc.txt" $ echo $STR|awk 'BEGIN{print substr($STR, 7)}'
27.
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4171295&page=1#pid24374435
文本如下:
1 R
S
T
2 FF
GG
HH
3 KKK
LLL
PPP
OOO
想要达到的目的:
1 R
1 S
1 T
2 FF
2 GG
2 HH
3 KKK
3 LLL
3 PPP
3 OOO
awk 'NF!=2{$0=s"\t"$1}{s=$1}1' file
……
awk '{print $1}' f2
1
S
T
2
GG
HH
3
LLL
PPP
OOO
awk 'NF!=2{print $1}' f2
S
T
GG
HH
LLL
PPP
OOO
awk 'NF==1{print $1}' f2
S
T
GG
HH
LLL
PPP
OOO
awk 'NF>1{print $1}' f2
1
2
3
awk 'NF!=2{$0=s"\t"$1}{s=$1;print s}' f2
1
1
1
2
2
2
3
3
3
3
awk '{s=$1;print s}NF!=2{$0=s"\t"$1}' f2
1
S
T
2
GG
HH
3
LLL
PPP
OOO
awk 'NF!=2{$0=s"\t"$1}{s=$1}1' f2
1 R
1 S
1 T
2 FF
2 GG
2 HH
3 KKK
3 LLL
3 PPP
3 OOO
awk 'NF!=2{$0=s"\t"$1}1' f2
1 R
S
T
2 FF
GG
HH
3 KKK
LLL
PPP
OOO
awk 'NF==2{s=$1}NF!=2{$0=s"\t"$1}1' f2
1 R
1 S
1 T
2 FF
2 GG
2 HH
3 KKK
3 LLL
3 PPP
3 OOO
awk '{s=$1}NF!=2{$0=s"\t"$1}1' f2
1 R
S S
T T
2 FF
GG GG
HH HH
3 KKK
LLL LLL
PPP PPP
OOO OOO
28.
http://bbs.chinaunix.net/thread-4171080-1-1.html awk怎么排除一列输出其他的列
cat file:
1|2|3|4|
怎么排除掉第三列输出
1|2|4|
awk -vRS="|" -v ORS="|" '{if(NR!=3)print $0}'
如果没有非空字段的话:
awk -F"|" '{$3="";gsub("[|]+","|",$0)}1' OFS="|"
awk -F'|' '{for(i=1;i<NF;i++)if(i!=3)printf $i"|";print ""}'
29.
http://bbs.chinaunix.net/thread-2300359-1-1.html bash之printf
http://bbs.chinaunix.net/thread-4165677-4-1.html 重复打印100次:
printf -- '%.0s-' {1..100};echo
----------------------------------------------------------------------------------------------------
length=100
eval printf -v line '%.0s-' {1..$length}
echo $line
----------------------------------------------------------------------------------------------------
awk 'BEGIN{while(i++<100)s=s"-";print s}'
----------------------------------------------------------------------------------------------------
seq 100|awk '{printf "-"}'
----------------------------------------------------------------------------------------------------
for i in $(seq 20);do echo -n abc;done
for i in `seq 20`;do printf abc;done
----------------------------------------------------------------------------------------------------
30.
http://bbs.chinaunix.net/thread-4157887-1-1.html
cat file:
>a1
1
>a1
11
>a1
111
>a2
2
>a2
22
>b1
3
>b1
33
将各个title 下边的合在一起 输出如下:
>a1
1
11
111
>a2
2
22
>b1
3
33
awk '(/^>/&&!a[$1]++)||/^[^>]/'
awk '/^>/&&!a[$1]++||!/^>/'
awk '{if(/^[>]/){t=$0} else {a[t]=a[t]"\n"$0}}END{for (i in a) print i,a[i]}'
awk '!a[$0]++'
awk '/>a1/{m++;x=NR}m==1&&NR-x<=1;m>1&&NR-x<=1&&NR-x>0;/>a2/{n++;y=NR}n==1&&NR-y<=1;n>1&&NR-y<=1&&NR-y>0;/>b1/{i++;j=NR}i==1&&NR-j<=1;i>1&&NR-j<=1&&NR-j>0'
___
aaa