我有一个从 .csv 转换而来的 .txt,如下所示
Smith","example","example","example","example"
example","Smith","example","example","example"
example","Smith","example","Smith","example"
example","example","example","Smith","example"
John","example","example","example","example"
example","example","example","John","example"
我只想保留包含单词Smith
or的行John
,但它们必须位于前两个字段内
输出应该是:
Smith","example","example","example","example"
example","Smith","example","example","example"
example","Smith","example","Smith","example"
John","example","example","example","example"
前两个字段可能不完全是John
or Smith
,它们可能是Johnson
例如,我仍然想保留它。
如果前两个字段不包含 John 或 Smith,则应删除该行。如果第一个或第二个字段包含它们,则无论如何都应保留该行(例如,如果整行包含“John”)
答案1
grep -E '^([^,]*,")?(Smith|John)' <infile
...将打印...
Smith","example","example","example","example"
example","Smith","example","example","example"
example","Smith","example","Smith","example"
John","example","example","example","example"
答案2
使用awk
:
< inputfile awk -F, '$1$2~/Smith|John/'
输出:
~/tmp$ cat inputfile
Smith","example","example","example","example"
example","Smith","example","example","example"
example","Smith","example","Smith","example"
example","example","example","Smith","example"
John","example","example","example","example"
example","example","example","John","example"
~/tmp$ < inputfile awk 'BEGIN {FS=","} $1~/Smith|John/||$2~/Smith|John/'
Smith","example","example","example","example"
example","Smith","example","example","example"
example","Smith","example","Smith","example"
John","example","example","example","example"