我有两个文本文件,A 和 B:
A:
a start
b stop
c start
e start
乙:
b
c
如何使用 shell(bash...) 命令找到 A 中的哪些行不是由 B 中的行开始的。在这种情况下,我想得到这个答案:
a start
e start
我可以使用单行命令来实现吗?
This should do:
sed '/^$/d;s/^/^/' B | grep -vf - A
The sed
command will take all non-empty lines (observe the /^$/d
command) from the file B
and prepend a caret ^
in front of each line (so as to obtain an anchor for grep
's regexp), and spits all this to stdout
. Then grep, with the -f
option (which means take all patterns from a file, which happens to be stdin
here, thanks to the -
symbol) and does an invert matching (thanks to the -v
option) on file A
. Done.
我认为应该这样做:
sed 's/^/\^/g' B > C.tmp
grep -vEf C.tmp A
rm C.tmp
You can try using a combination of xargs
, cat
, and grep
Save the first letters of each line into FIRSTLETTERLIST. You can do this with some cat
and sed
work.
The idea is to take the blacklist and then match it against the interesting file.
cat file1.txt | xargs grep ^[^[$FIRSTLETTERLIST]]
This is untested, so I won't guarantee it will work, but it should point you in the right direction.