私は次のファイルを持っています
Hello. this is an example
Car=toyota Fruit=Orange Name=John
01
Car=toyota Fruit=Orange Name=John
02
Car=toyota Fruit=Orange Name=John
03
End of the file
このawkコードをシェルで実行します
awk -F "t" "{n=split($2,a); for(i=1 ; i<=n ; i++) {if(a[i] ~ "^Fruit*") a[i]=""} print}" myFile.txt
したがって、フィールドセパレータは tab
。次に、2番目のフィールドを使用して分割します space
。 (スペースで区切られた)サブフィールドのいずれかが Fruit
削除したい。
これは機能しません。削除されません。
期待されるアウトプット
Hello. this is an example
Car=toyota Name=John
01
Car=toyota Name=John
02
Car=toyota Name=John
03
End of the file
余分なパッケージを使用しないでください。できるだけデフォルトにしたい。 (awk、sed、grepを使用すると素晴らしいでしょう)
回答:
回答№1の場合は3(スペースで区切られた)サブフィールドのいずれかが
Fruit
削除したい。
あなたは使うことができます sed
:
$ sed "s/Fruit=[^ ]* //g" inputfile
Hello. this is an example
Car=toyota Name=John
01
Car=toyota Name=John
02
Car=toyota Name=John
03
End of the file
回答№2については2
awk "{gsub(/ Fruit[^ ]*/,"")}1" myFile.txt
与える:
Hello. this is an example
Car=toyota Name=John
01
Car=toyota Name=John
02
Car=toyota Name=John
03
End of the file
答え№3の2
$ cat file
Hello. this is an example
Car=toyota ForeName=John Name=JohnSmith
01
End of the file
$ sed -r "s/(^|[[:space:]])ForeName=[^[:space:]]+[[:space:]]*/1/" file
Hello. this is an example
Car=toyota Name=JohnSmith
01
End of the file
$ sed -r "s/(^|[[:space:]])Name=[^[:space:]]+[[:space:]]*/1/" file
Hello. this is an example
Car=toyota ForeName=John
01
End of the file