最初の2つのフィールドにXを含む行を保持する

最初の2つのフィールドにXを含む行を保持する

以下のように.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または、単語を含む行のみを維持したいが、John最初の2つのフィールド内になければなりません。

出力は次のようになります。

Smith","example","example","example","example"
example","Smith","example","example","example"
example","Smith","example","Smith","example"
John","example","example","example","example"

最初の 2 つのフィールドはまさにJohnOR ではないかもしれませんSmithJohnson

最初の2つのフィールドにJohnまたはSmithが含まれていない場合は、行を削除する必要があります。最初または2番目のフィールドにそのエントリが含まれている場合、行はそのままにしてください(たとえば、行全体に「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"

関連情報