为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

zx96895 2018-11-05 01:03:17
文件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
查看完整描述

3 回答

?
zx96895

TA贡献2条经验 获得超1个赞

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

 


查看完整回答
1 反对 回复 2018-11-19
?
慕瓜9220888

TA贡献868条经验 获得超0个赞

PLFVALYMG

ZLHOUPQXY

RHTSYSJNL

DPLDJNKOS

IUTZZVSQK

OGIFTWRJJ

TPZVUFZCA

UGWDTDOBP

SEOQFYCPS

BWBUPSSFJ

KJRTHRXKX

RFEJFPVPB

PUZRUATAN

YTOASMZCV

WACEADKXP

IGFHDMZZZ

EQLRJWWJZ

KVVHZJPSN

BMUUPHEKE

PKPUGJNAW

DYUMEBELL

ACPUAOOSJ

GEUUDREKY

IADJIWUXA

UQGCLITQG

EWAJSDDKL

LWWSUIWWA

VYENJHVIM

BWSBRPTGM

FGGMSMGTE

YKDBHLVIM

WOEJTGUYM

DLBCICQUY

AWCRQHDXG

OZHHSWCUV

CNSYWNGZK

HYHDJTEYY

DLQPUOLFO

ZUPSKACFY

AQCKWJDZD

AEFHDGURB

PQVLYNKHU

GRFIANWTW

SCETLYYFS

JSYTPPVCV

QEXXNROEY

MORXJFCPP

MQJOGTTGQ

BZMYHKANG

MTZLTXNKT

SNDJZTBCW

TREWGKNDN

BZIGJJDZG

SDPRNAXQM

KCHJPCFCE

BWJVUHUAZ

XYQJOBLSY

NDJARIMJD

IRAEQAWTT

SNBMOSOYS

RMLQIFTQA

JUQSPSQDG

CUGTSPMFI

GRUXQMMJM

DIJUGJGJQ

MBDONRERX

PKGCEKEXE

VGSLBEXHD

JNZBNKNQA

AOOJBIVPV

WJZOEFIMN

IAEQZXBOM

SKQQZDIMG

OGSIOSVPO

JFJCSZDXB

IEDJZNNBL

SDCNPDHUI

FTCLYBFTH

NFYUKERRV

UTNJRFCQE

BKWCSOBPZ

JRDQHBFDA

WLHUQTEBZ

IAACHDXUN

RXDSVLYYR

XVKPIEREY

DEGKPCWTC

LJTFLOKXB

KJZLXOVMC

XMVBHDABN

NNZZPXIFD

PNQYELJJU

QKQCVUHLG

FUQEUOOCT

BWMCKLPDN

DFBHNNRFP

LGODTPGJU

UZCLLPCQN

CREDKESCE

ZXNXTAEQW

TJVXUYSVS

CIVAGUOIP

XCOUEBPKK

LEDMSGKHB

TVKUZCWJM

HPSCRURBA

XYUWSPBDQ

BTLHGTQJA

FWGCGJMJC

DODHDWXTN

CXGMVLUBU

OHNTSGYWN

YSUKPSPFC

NFHLXNWDN

ZLQSKORWM

GFAMVOVOA

XIKHQGXQN

JXZFNRHKH

HZDJJDDLL

ENZIOSBPJ

YXUKAUUGA

QYLRXCQXV

WMMWLWZAE

DBBSOPZDA

GIUEISQTN

FEAMZTGOF

FIXBKIMZM

TYUKAHLKB

IXDTPDAUO

AZFIEMRFC

PHEGILVFF

NZRMQQGTD

QIVBBVYJD

VACPPSLBY

QBTCNKRSZ

TNWBUIMZS

VKDWBTWTW

ATNHUYYIG

YTVBREVIV

WKGFUXUDU

FJFRTMGBO

SUGLHRNAR

FNNCLBEGC

IQMPVHYBU

VUEQIFJNR

CEAZZJGKW

SNTTJGURY

SRTTBBLZD

AIMCOVJNX

PEUTZFPTX

LJZISCOFD

TBRRWKELM

JSFVUCFJL

XDDZZXHLV

CRBNBLWKU

GOLVUVFTT

PKKGBVOGZ

DRNJQHOBI

ZTSOQJWTG

BTCCKOPIF

RZFFHBVPA

VASGVVIVL

KMEAVPCFY

CNTMHKFFW

MGFOAWGDW

XMJCXKROH

FTDMYVVFG

CNRNDPJHR

GKCEXCFSR

KOKWNAGAG

WRCFHPPFJ

GOZTJMPMV

VGMLQDNQJ

JKLNPMPWZ

HIUQLPSFN

NPLHGNJWT

NSIKMRRLE

PAZLNCLYL

NCOHPMPOR

LQPVDKISP

查看完整回答
反对 回复 2021-10-20
?
zx96895

TA贡献2条经验 获得超1个赞

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

查看完整回答
反对 回复 2018-11-06
?
pardon110

TA贡献1038条经验 获得超227个赞

如果只是对比用diff吧

查看完整回答
反对 回复 2018-11-05
  • 3 回答
  • 0 关注
  • 2115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信