开发者

Shell compare and mark

I have 2 files list.txt and fail.txt. Its a list of test cases and failed test cases. I need to mark failed against the test cases which appear in fail.txt in the list.txt. I ne开发者_开发知识库ed it in shell


Maybe you should investigate the join command?

sed 's/$/ FAIL/' fail.txt |
join -a 1 -e PASS -j 1 -o 1.1,2.2 list.txt -

list.txt

/work/supernova/alpha/net-nsf.c
/work/supernova/alpha/net-nsf.h
/work/supernova/alpha/nsfpipe.c
/work/supernova/alpha/sqcrypto.c
/work/supernova/alpha/sqlmutil.c
/work/supernova/alpha/squtil.c
/work/supernova/beta/scb.h
/work/supernova/delta/delta-init.c
/work/supernova/delta/delta-pseudo.c
/work/supernova/delta/onvproc.c
/work/supernova/gamma/mt-aio.c
/work/supernova/gamma/mt-aio.h
/work/supernova/gamma/mtblk.c
/work/supernova/gamma/mt.c
/work/supernova/gamma/mtdll.c
/work/supernova/gamma/mt.h
/work/supernova/gamma/mt-info.c
/work/supernova/gamma/mtm.h
/work/supernova/gamma/mtpool.c
/work/supernova/gamma/mtpseudo.c
/work/supernova/gamma/session.c
/work/supernova/gamma/session.h
/work/supernova/omega/jlm.c
/work/supernova/omega/osenv.c
/work/supernova/omicron/mivp.c

fail.txt

/work/supernova/alpha/net-nsf.c
/work/supernova/alpha/net-nsf.h
/work/supernova/alpha/sqcrypto.c
/work/supernova/beta/scb.h
/work/supernova/delta/delta-init.c
/work/supernova/delta/delta-pseudo.c
/work/supernova/gamma/mt-aio.c
/work/supernova/gamma/mt-aio.h
/work/supernova/gamma/mtblk.c
/work/supernova/gamma/mt-info.c
/work/supernova/gamma/mtpool.c
/work/supernova/gamma/session.h
/work/supernova/omega/jlm.c

Output

/work/supernova/alpha/net-nsf.c FAIL
/work/supernova/alpha/net-nsf.h FAIL
/work/supernova/alpha/nsfpipe.c PASS
/work/supernova/alpha/sqcrypto.c FAIL
/work/supernova/alpha/sqlmutil.c PASS
/work/supernova/alpha/squtil.c PASS
/work/supernova/beta/scb.h FAIL
/work/supernova/delta/delta-init.c FAIL
/work/supernova/delta/delta-pseudo.c FAIL
/work/supernova/delta/onvproc.c PASS
/work/supernova/gamma/mt-aio.c FAIL
/work/supernova/gamma/mt-aio.h FAIL
/work/supernova/gamma/mtblk.c FAIL
/work/supernova/gamma/mt.c PASS
/work/supernova/gamma/mtdll.c PASS
/work/supernova/gamma/mt.h PASS
/work/supernova/gamma/mt-info.c FAIL
/work/supernova/gamma/mtm.h PASS
/work/supernova/gamma/mtpool.c FAIL
/work/supernova/gamma/mtpseudo.c PASS
/work/supernova/gamma/session.c PASS
/work/supernova/gamma/session.h FAIL
/work/supernova/omega/jlm.c FAIL
/work/supernova/omega/osenv.c PASS
/work/supernova/omicron/mivp.c PASS

As long as the files are in sorted order, should work like a charm. If you don't want the PASS marks, then omit the -e PASS argument pair. If you want a different FAIL mark, adjust the sed script. If you want a different order for the output columns, alter the -o option.


you might want to have a go with grep and its -f and -v option

grep -v -f file1 file2
grep -v -f file2 file1


without you sharing the format of your files, here's a guess:

awk '
    NR == FNR {failed[$1]; next} 
    ($1 in failed) {print "failed: " $0; next} 
    1
' fail.txt list.txt > marked.txt

update:

awk '
    NR == FNR {failed[$1]; next} 
    {
      status = ($1 in failed) ? "Fail" : "Pass"
      print $0, status
    }
' fail.txt list.txt > marked.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜