我有两个文件。我想列出一个不在另一个文本文件中的文本文件的内容

时间:2017-12-03 17:28:10

标签: linux

例如,

名为ppcc.txt的文本文件的内容为:

SUBSCRIBERIDENTIFIER|234818624|2000018
SUBSCRIBERIDENTIFIER|234817904|5098596
SUBSCRIBERIDENTIFIER|234908279|5093267
SUBSCRIBERIDENTIFIER|234908023|VOIPBurn
SUBSCRIBERIDENTIFIER|234909392|VOIPBurn

另一个名为pcs.txt的文件的内容是:

234818947|1754261815|20370101000000
234809110|358155618|20370101000000
234809417|1385298890|20361231230000
234908023|1381142974|20171124205011
234909392|358155618|20370101000000

我正在搜索234号码,我想将我的输出保存在另一个文本文件中作为Result.txt,其中包含以下内容:

SUBSCRIBERIDENTIFIER|234818624|2000018
SUBSCRIBERIDENTIFIER|234817904|5098596
SUBSCRIBERIDENTIFIER|234908279|5093267

1 个答案:

答案 0 :(得分:0)

使用awk

awk 'BEGIN{FS=OFS="|"}FNR==NR{a[$1]=1;next} !($2 in a)' pcs.txt ppcc.txt

我们正在阅读两个文件,首先是pcs.txt,然后是ppcc.txt。程序说明:

# Set input and output field delimiter to |
BEGIN{FS=OFS="|"}

# As long as FNR (record number in current input file) and 
# NR (overall record number) are equals we are reading the 
# first file - pcs.txt
FNR==NR{
    # Store the value of the first column in as a key in
    # an array "a"
    a[$1]=1
    # Don't process any further blocks. Continue with the 
    # next line of input
    next
}

# This expression is only executed on lines of ppcc.txt
# If the array "a" doesn't not contain the second column
# as a key, print it.
!($2 in a)