awk數組處理兩個檔案的例子
如果檔案a中包含檔案b,則將檔案b的記錄列印出來輸出到c檔案裡
檔案a:
10/05766798607,11/20050325191329,29/0.1,14/05766798607
10/05767158557,11/20050325191329,29/0.08,14/05767158557
檔案b:
05766798607
05766798608
05766798609
通過檔案a和檔案b對比,匯出這樣的檔案出來.
10/05766798607,11/20050325191329,29/0.1,14/05766798607 本人查了很多網上的答案都是錯誤碼的 正確答案應該: 方法一: awk -F'[/,]' 'ARGIND==1{a[$0]}ARGIND>1{if ($2 in a)print $0}' b a >c 方法二: awk -F'[/,]' 'NR==FNR{a[$0]}NR>FNR{if ($2 in a) print $0}' b a >c 這兩種方法是用數組處理的,速度比較快,處理9萬行只需4秒。 還有一種方法是通過while 每次用read 命令從b中讀一條記錄與a中$2比較如果相等則輸出到c中 root@TestAs4
zlwt]# more for3.sh
#!/bin/bash
while read line ; do
awk -F'[/,]' '$2 == '$line' {print $0}' a >>cdone < b; 這種方法很好理解,但速度非常慢,每次唯讀取一條記錄,9萬行需5個小時處理。 例二
awk數組處理兩個檔案索引的問題(替代法)
[root@TestAs4 zlwt]# more a
deptA
deptB
deptC
deptD
[root@TestAs4 zlwt]# more b
aaa 0
bbb 1
ccc 2
ddd 0
eee 2
fff 2
[root@TestAs4 zlwt]# awk 'NR==FNR {k[i++]=$1} NR>FNR { print $1,k[$2]}' a b
aaa deptA
bbb deptB
ccc deptC
ddd deptA
eee deptC
fff deptC NR==FNR {k[i++]=$1} #先把a檔案的值賦給數組k,下標從0自動成長 NR>FNR { print $1,k[$2] #其中 $1,$2是b中的第一,二個域,k[$2]為a的值 下面方法是r2007版主的其實是一樣的
[root@TestAs4 zlwt]# awk '{if(NR==FNR)k[i++]=$0;else print $1,k[$2]}' a b
aaa deptA
bbb deptB
ccc deptC
ddd deptA
eee deptC
fff deptC 另外一個例子 awk
' BEGIN{FS="[|]";OFS="|"}
FNR==NR{a[$1]=$2}
FNR<NR{if(!a[$1]) {$1="13";print}
else {$1=a[$1];print}}
' wj wj1>wj2
檔案1
1|name1
2|name2
3|name3
5|name5
6|name6
檔案2
1|name11
2|name22
3|name33
4|name44
5|name55
6|name66
7|name77
8|name88
輸出結果
name1|name11
name2|name22
name3|name33
13|name44
name5|name55
name6|name66
13|name77
13|name88 它在處理2個以|分割的檔案
例如
檔案1 wj 格式
id1|desc1
檔案2 wj1格式
id2|desc2
FNR==NR{a[$1]=$2} 意思是處理第一個檔案時 把 desc1 賦值給 數組 a 的 a[id1] 單元。
FNR<NR 條件是在處理第2檔案成立。這樣在處理第2 檔案時
{if(!a[$1]) {$1="13";print}
else {$1=a[$1];print
如果a[$1] 是空,就把第2檔案那行的第1列替換為 13 輸出 如: 13|desc2
如果a[$1]非空,就是這個數組值已經在處理第1檔案賦過值。就把$1替換為 a[$1] 即 檔案1對應的$2。輸出的就是 desc1|desc2
歸納一句 就是在檔案2中以id2在檔案1中查id1=id2的對應desc1 ,
找到輸出 desc1|desc2
找不到輸出 13|desc2 例:把數組中如1331131***** 批量替換成86
1331131*****
#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@TestAs4 cwm]# awk '{print $1,substr($1,1,7)}' hlj_jifei >hlj_temp
[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
或
[root@TestAs4 mdn]# awk 'NR==FNR{a[$1]=$4}NR>FNR&&a[b=substr($1,1,7)]{print $1,a[b]}' test1 hlj_temp
13009700055 哈爾濱
13009700495 哈爾濱
13009701075 哈爾濱
13009701282 哈爾濱
--------------------------------------------------------------------------------------
[root@TestAs4 mdn]# more temp
1300970 13009700055
1300970 13009700495
1300970 13009701075
1300970 13009701282
--------------------------------------------------------------------------------
[root@TestAs4 mdn]# more awk_script
BEGIN { while ((getline < "test1") > 0){ lines[$1]=$4 };OFS=" " }
{
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歸類。
-----------------------找出兩檔案相同及不同的值
----------------------------------
awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' file1 file2 找出檔案2中不同的值
awk 'NR==FNR{a[$0]++} NR>FNR&&a[$0]' file1 file2 找出兩檔案中相同的值
或
awk 'NR==FNR{a[$0]}NR>FNR{ if(!($1 in a)) print $0}' file1 file2 找出檔案2中不同的值
awk 'NR==FNR{a[$0]}NR>FNR{ if($1 in a) print $0}' file1 file2 找出兩檔案中相同的值
------------------------awk按欄位分類統計
----------------------------------------
1300018 廣東
1300019 廣東
1300100 北京
1300101 北京
1300126 北京
1300127 北京
1300128 北京
1300129 北京
1300130 天津
1300131 天津
1300132 天津
1300133 天津
想得到三個檔案:
廣東2.txt
1300018
1300019
北京6.txt
1300100
1300101
1300126
1300127
1300128
1300129
天津4.txt
1300130
1300131
1300132
1300133
awk '{a[$2]++;print $1 > $2} END {for (i in a) {print "mv " i " " i""a[i]".txt" }}' ufile|sh