归纳一句 就是在文件2中以id2在文件1中查id1=id2的对应desc1 ,
找到输出 desc1|desc2
找不到输出 13|desc2
例:把数组中如1331131***** 批量替换成861331131*****
#cat a.txt
13994623***
13394660***
13394660***
13394671***
13394672***
13394690***
13394692***
15304863***
#awk '{print "86"$1}' a.txt > b.txt
8613994623***
8613394660***
8613394660***
8613394671***
8613394672***
8613394690***
8613394692***
8615304863***
#awk '{print substr($1,3,11)}' b.txt 把86去掉
13994623***
13394660***
13394660***
13394671***
13394672***
13394690***
13394692***
15304863***
------------------------------------------------------------------------------
两个文件关联处理
[root@TestAs4 cwm]# awk '{print $1}' 153mdn.txt |uniq -c
4 七台河
5 伊春
18 佳木斯
13 双鸭山
66 哈尔滨
1 大兴安岭
32 大庆
20 牡丹江
19 绥化
16 鸡西
15 鹤岗
10 黑河
19 齐齐哈尔
[root@mos download]# awk -f: {'print $1,$nf'} /etc/passwd|tail -1。[root@andy ~]# netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -f: '{print $4}'|sort|uniq -c|sort -nr|head -20。[kodango@devops ~]$ awk 'begin { print substr("kodango", 2, 3)。
[root@TestAs4 mdn]# more hlj_temp
13009700055 1300970
13009700495 1300970
13009701075 1300970
13009701282 1300970
[root@TestAs4 mdn]# ls
2 3 awk_script cwm hlj_jifei hlj_temp newmdn_table.TXT temp test1
[root@TestAs4 mdn]# more test1
1300019 510 020 广州
1300101 110 010 北京
1300103 110 010 北京
1300104 110 010 北京
1300106 110 010 北京
[root@TestAs4 mdn]# awk 'NR==FNR{a[substr($1,1,7)]=$4}NR>FNR&&a[b=substr($1,1,7)]{print $1,a[b]}' test1 hlj_temp |more
或
awk---grep---sed [root@book class]# df -k | awk '{print $1}' | grep -v 'none' | sed s"/\/dev\///g"。[root@tx3 ~]# awk 'begin{"date"| getline a}{print}end{print a}' cj。[kodango@devops ~]$ awk 'begin { print substr("kodango", 2)。
13009700055 哈尔滨
13009700495 哈尔滨
13009701075 哈尔滨
13009701282 哈尔滨
--------------------------------------------------------------------------------------
[root@TestAs4 mdn]# more temp
1300970 13009700055
1300970 13009700495
1300970 13009701075
1300970 13009701282
--------------------------------------------------------------------------------
[root@TestAs4 mdn]# more awk_script
$ awk 'begin { while("cat statement.txt" | getline) print $0,nf}'statement 1delete 1exit 1next 1。[kodango@devops awk_temp]$ awk 'begin { while("cat statement.txt" | getline var) print var}' 。行距步长g92 x0 y0 z[#13+20]g90g00 x[#10/2] y[#11/2] m03g01 z0x[-#10/2] y[#11/2]g17g01 x[-#10/2] y[-#11/2]x[#10/2]y[#11/2]#0=#10/2#1=-#0#2=#13-#14#5=#12*sqrt[1-#2*#2/#13/#13]g01 z[#14]while #0 ge #1if abs[#0] lt #5#3=#13*sqrt[1-#0*#0/[#12*#12]]if #3 gt #2#4=sqrt[#3*#3-#2*#2]g01 y[#4] f400g19 g03 y[-#4] j[-#4] k[-#2]endifendifg01 y[-#11/2] f400#0=#0-#15g01 x[#0]if abs[#0] lt #5#3=#13*sqrt[1-#0*#0/[#12*#12]]if #3 gt #2#4=sqrt[#3*#3-#2*#2]g01 y[-#4] f400g19 g02 y[#4] j[#4] k[-#2]endifendifg01 y[#11/2] f1500#0=#0-#15g01 x[#0]endwg00 z[#13+20] m05g00 x0 y0m30。
{
if($1 in lines){
$1=lines[$1] #把test1文件的$4替换到temp文件的$1上

print $0
}
}
#要求把test1文件的第四个字段插入到temp文件的相应条目的第一个子段中
#利用getline获取test1文件的第四个字段,并且放到一个数组中。
[root@TestAs4 mdn]# ls
2 3 awk_script cwm hlj_jifei hlj_temp newmdn_table.TXT temp test1
[root@TestAs4 mdn]# awk -f awk_script temp |wc -l
63440
[root@TestAs4 mdn]# awk -f awk_script temp |more
哈尔滨 13009700055
哈尔滨 13009700495
awk又一个例子: 统计某一列所有值的和
把所有第二列的值求和
[root@TestAs4 ~]# more cwm.txt
cwm 123
zbl 124
yhh 2
cj 1
[root@TestAs4 ~]# awk '{a[x++]=$2};END{for(i=1; i<=NR; i++) b=b+a[i-1];print b }' cwm.txt
250
[root@TestAs4 ~]# awk '{a[NR]=$2;b=0};END{for(i=1; i<=NR; i++) b=b+a[i];print b }' cwm.txt
250
显示文件的从第m行到n行
[root@TestAs4 ~]# sed -n '2,10'p mdn.txt
[root@TestAs4 ~]# awk 'NR==2,NR==10{print $0}' mdn.txt
给手机号码分G网C网
1.C网(C网是133或153开头的号)
awk '$1 ~/^133/ || $1 ~/^153/' file.txt >C网.txt
2.G网(由于G网比较多非133非153开头的都是)
awk '$1 !~/^133/ && $1 !~/^153/' file.txt >G网.txt
给两个文件每行对应连接
[root@TestAs4 cwm]# more tep_01.txt
cwm 13911320988
zbl 13931095233
chen 12333333333
cwm 12233333333
cwm 45555555555
[root@TestAs4 cwm]# more tep_02.txt
cwm1 111320988
zbl1 131095233
chen1 133333333
cwm1 133333333
cwm1 455555555
awk 'NR==FNR {a[FNR]=$0} NR>FNR { print $0,a[FNR]}' tep_01.txt tep_02.txt
cwm1 111320988 cwm 13911320988
zbl1 131095233 zbl 13931095233
chen1 133333333 chen 12333333333
cwm1 133333333 cwm 12233333333
cwm1 455555555 cwm 45555555555
还有一个命令 paste
[root@TestAs4 cwm]# paste tep_01.txt tep_02.txt
cwm 13911320988 cwm1 111320988
zbl 13931095233 zbl1 131095233
chen 12333333333 chen1 133333333
cwm 12233333333 cwm1 133333333
cwm 45555555555 cwm1 455555555
awk 处理HAN开头下一个HAN的上一行数字为结尾的文件 ... 或者中提取任一文件段 以HAN开头,下一个HAN的上一行数字段为结尾的一段 生成HAN1等这样的文件
[root@TestAs4 cwm]# more file1.txt
HAN 1
12 23 34 45
23 45 56
HAN 2
12 23 34 45
23 45 56
12 23 34 45
HAN 3
12 23 34 45
23 45 56 44
12 23 34 45
23 45 56
HAN 4
12 23 34 45
23 45 56
HAN n
awk '{ if ($1=="HAN" && NF==2) fn=$2; print $0>>"HAN" fn;}' file1.txt
awk '{fn=$2; print $1 >>fn"hb"}' hbuse.txt 这是所有记录以$2归类。
-----------------------找出两文件相同及不同的值----------------------------------
cp file1 file2上述cp命令复制文件file1.php 的内容到文件file2.php中。如果一个站点下有2个虚拟目录myweb1和myweb2,myweb1下有文件file1.asp,myweb2下有文件file2.asp,如果file1.asp要调用file2.asp,那么在file1.asp中要这样写:<。为真的前提是 file1 比 file2 新或者 file1、file2 中有一个文件不存在(应该使用绝对路径)。
awk 'NR==FNR{a[$0]++} NR>FNR&&a[$0]' file1 file2 找出两文件中相同的值
或
当中file1.c中使用的double函数时本文件里定义的内联函数,而在file2.c和file3.c中使用的double函数确实file2.c中定义的外部函数。如果一个站点下有2个虚拟目录myweb1和myweb2,myweb1下有文件file1.asp,myweb2下有文件file2.asp,如果file1.asp要调用file2.asp,那么在file1.asp中要这样写:<。为真的前提是 file1 比 file2 新或者 file1、file2 中有一个文件不存在(应该使用绝对路径)。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-114808-5.html
果然全宇宙最火
再会赌的赌徒