我有两个文件-file1和file2。 file1包含(仅单词)说-
export const StyledInput = styled.input`
//Reset box-shadow
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
display: block;
padding: 0.75em 2em 0.75em 0.75em; //Extra padding on the right for the clear button.
font-size: 1em;
//Hide the standard clear button.
&[type=search]::-webkit-search-cancel-button {
display: none;
}
`;
export const StyledClearButton = styled.button`
position: absolute;
top: calc(2.8em - env(safe-area-inset-top)); //env() is for the nodge on iOS.
right: 0;
`;
const StyledInputSpan = styled.span`
display: flex;
align-items: center;
`;
..................
file2包含许多段。
ABC
YUI
GHJ
I8O
...................
我正在使用以下命令来获取包含file2中file1中单词的匹配行
dfghjo ABC kll njjgg bla bla
GHJ njhjckhv chasjvackvh ..
ihbjhi hbhibb jh jbiibi
我还需要在文件2中不匹配/找不到并且无法找到不匹配单词的单词。
任何人都可以帮助获得低于输出的水平
grep -Ff file1 file2
(Gives output of lines where words of file1 found in file2)
我正在寻找一个班轮命令(通过grep,awk,sed),因为我使用的是pssh命令,不能在while循环中使用
答案 0 :(得分:1)
您只能使用-o
打印匹配的部分。
$ grep -oFf file1 file2
ABC
GHJ
使用该输出作为文件1中搜索的模式列表。进程替换<(cmd)
模拟包含cmd
输出的文件。使用-v
可以打印不匹配的行。如果file1包含两行,使得其中一行是另一行的子字符串,则可能需要添加-x
(仅匹配整行)以防止误报。
$ grep -vxFf <(grep -oFf file1 file2) file1
YUI
I8O
答案 1 :(得分:1)
使用Perl-在同一单行代码中匹配/不匹配
$ cat sinw.txt
ABC
YUI
GHJ
I8O
$ cat sin_in.txt
dfghjo ABC kll njjgg bla bla
GHJ njhjckhv chasjvackvh ..
ihbjhi hbhibb jh jbiibi
$ perl -lne '
BEGIN { %x=map{chomp;$_=>1} qx(cat sinw.txt); $w="\\b".join("\|",keys %x)."\\b"}
print "$&" and delete($x{$&}) if /$w/ ;
END { print "\nnon-matched\n".join("\n", keys %x) }
' sin_in.txt
ABC
GHJ
non-matched
I8O
YUI
$
仅获取不匹配的内容
$ perl -lne '
BEGIN {
%x = map { chomp; $_=>1 } qx(cat sinw.txt);
$w = "\\b" . join("\|",keys %x) . "\\b"
}
delete($x{$&}) if /$w/;
END { print "\nnon-matched\n".join("\n", keys %x) }
' sin_in.txt
non-matched
I8O
YUI
$
请注意,在Perl版本$& variable中,即使一次使用prior to 5.20对于整个程序来说也是非常昂贵的。
答案 2 :(得分:0)
假设file1中的“单词”多于1行:
while read line
do
for word in $line
do
if ! grep -q $word file2
then echo $word not found
fi
done
done < file1
答案 3 :(得分:0)
对于不匹配的单词,这是一种GNU awk
解决方案:
awk 'NR==FNR{a[$0];next} !($1 in a)' RS='[ \n]' file2 file1
YUI
I8O
还是!($0 in a)
,都一样。由于我设置了RS='[ \n]'
,所以每个空格也都是 line 分隔符。
请注意,我先读取file2,然后读取file1。
如果file2可能为空,则应将NR==FNR
更改为其他文件检查方法,例如GNU awk的ARGIND==1
,FILENAME=="file2"
或FILENAME==ARGV[1]
等。
对仅匹配的也有相同的机制:
awk 'NR==FNR{a[$0];next} $0 in a' RS='[ \n]' file2 file1
ABC
GHJ