2016-09-17 4 views
1

を含まないすべての行に置き換え:私ははないに文字列が含まれないすべての行を交換しようとしている私はこのようになりますこのデータのファイルで働いているマッチした文字列

text in file 
hello random text in file 
example text in file 
words in file hello 
more words in file 
hello text in file can be 
more text in file 

hellomatchでsedを使用して、その出力は次のようになります。

match 
hello random text in file 
match 
words in file hello 
match 
hello text in file can be 
match 

私はsed '/hello/!d'を使用してみましたが、それは行を削除します。また、私はsedの中で!を使用して一致させることができますが、私はどのように各行をマッチさせて適切に置き換えるのかはわかりません。あなたが私にある方向性を与えることができれば、本当に感謝します。

答えて

3

あなたはこのようにそれを行うことができます。

$ sed '/hello/!s/.*/match/' infile 
match 
hello random text in file 
match 
words in file hello 
match 
hello text in file can be 
match 

/hello/!たちはhelloを含まない行にのみ置き換えているを確認します(.*)(あなたは右のそれだった)、および置換は、完全なパターンスペースを置き換えますmatchとなります。

+0

これは完全に機能します。私を助けてくれてありがとう。意味あり。あなたが1分を受け取ったら、アップしてください。 – DomainsFeatured

6

awkレスキュー!

$ awk '!/hello/{$0="match"}1' file 

"hello"と一致しない行を "match"に置き換え、すべての行を置換します。

+0

行を置き換える。 – DomainsFeatured

+0

すべての行を出力するには '1'が必要です。 – karakfa

+0

ああ、あなたは正しいです。ありがとうございました。私はawkとあまりよくはありませんが、これは素晴らしい作品です。 – DomainsFeatured

0

だけc(変化する)コマンドでセッド:

awk '{print (/hello/ ? $0 : "match")}' file 
3

など、分かりやすく、簡単にするためのawkを使用:ねえKarakfaが、これは良いのですが、加えて他の行を削​​除している

$ sed '/hello/!c match' file 
match 
hello random text in file 
match 
words in file hello 
match 
hello text in file can be 
match 
関連する問題