如何用grep,sed,awk对比两个文件输出指定信息?

文件1内容:

[lv_edb14adata_rmeta_0]    sdp
[lv_edb14adata_rmeta_1]     sdd
[lv_edb14aarch_rmeta_0]     sdx
[lv_edb14aarch_rmeta_1]      sdaf
[lv_edb14axlog_rmeta_0]      sdz
[lv_edb14axlog_rmeta_1]      sdah
[lv_edb20adata_rmeta_0]      sdr
[lv_edb20adata_rmeta_1]     sdal
[lv_edb20aarch_rmeta_0]       sdaa
[lv_edb20aarch_rmeta_1]       sdai
[lv_edb20axlog_rmeta_0]      sdac
[lv_edb20axlog_rmeta_1]      sdak

文件2内容:

sdd   5800h
sde   5800h
sdh   5800h
sdi    5800h
sdm   5800h
sdn    5800h
sdo    5800h
sdad   5800h
sdaf   5800h
sdag   5800h
sdah   5800h
sdai    5800h
sdaj    5800h
sdak   5800h
sdp    5800g
sdq    5800g
sdr    5800g
sds    5800g
sdt    5800g
sdu    5800g
sdv    5800g
sdw    5800g
sdx    5800g
sdy    5800g
sdz    5800g
sdaa   5800g
sdab   5800g
sdac   5800g
sdal    5800h
sdae    5800g

通过比对文件1的第二列和文件二的第一列,输出结果:

[lv_edb14adata_rmeta_0]    sdp    5800g
[lv_edb14adata_rmeta_1]    sdd   5800h
[lv_edb14aarch_rmeta_0]   sdx    5800g
[lv_edb14aarch_rmeta_1]   sdaf   5800h
[lv_edb14axlog_rmeta_0]   sdz     5800g
[lv_edb14axlog_rmeta_1]     sdah    5800h
[lv_edb20adata_rmeta_0]     sdr     5800g
[lv_edb20adata_rmeta_1]    sdal    5800h
[lv_edb20aarch_rmeta_0]     sdaa    5800g
[lv_edb20aarch_rmeta_1]     sdai      5800h
[lv_edb20axlog_rmeta_0]     sdac     5800g
[lv_edb20axlog_rmeta_1]     sdak    5800h

zx96895
浏览 2122回答 3
3回答

zx96895

awk 'BEGIN{FS=OFS=" "}NR==FNR{w[$1]=$2}NR>FNR{for(a in w) if(a==$2){print $1,$2,w[a]; break}}' file2 file1 

慕瓜9220888

PLFVALYMGZ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DLLENZIOSBPJYXUKAUUGAQYLRXCQXVWMMWLWZAEDBBSOPZDAGIUEISQTNFEAMZTGOFFIXBKIMZMTYUKAHLKBIXDTPDAUOAZFIEMRFCPHEGILVFFNZRMQQGTDQIVBBVYJDVACPPSLBYQBTCNKRSZTNWBUIMZSVKDWBTWTWATNHUYYIGYTVBREVIVWKGFUXUDUFJFRTMGBOSUGLHRNARFNNCLBEGCIQMPVHYBUVUEQIFJNRCEAZZJGKWSNTTJGURYSRTTBBLZDAIMCOVJNXPEUTZFPTXLJZISCOFDTBRRWKELMJSFVUCFJLXDDZZXHLVCRBNBLWKUGOLVUVFTTPKKGBVOGZDRNJQHOBIZTSOQJWTGBTCCKOPIFRZFFHBVPAVASGVVIVLKMEAVPCFYCNTMHKFFWMGFOAWGDWXMJCXKROHFTDMYVVFGCNRNDPJHRGKCEXCFSRKOKWNAGAGWRCFHPPFJGOZTJMPMVVGMLQDNQJJKLNPMPWZHIUQLPSFNNPLHGNJWTNSIKMRRLEPAZLNCLYLNCOHPMPORLQPVDKISP

zx96895

awk 'BEGIN{FS=OFS=" "}NR==FNR{w[$1]=$2}NR>FNR{for(a in w) if(a==$2){print $1,$2,w[a]; break}}' file2 file

pardon110

如果只是对比用diff吧
打开App,查看更多内容
随时随地看视频慕课网APP