:とコロンの間に最初に現れる文字列を抽出する方法

:とコロンの間に最初に現れる文字列を抽出する方法

データベースに入力する前に再処理する必要がある非常に長いファイルがあります。このファイルのデータ型は次のとおりです。

Error for: 111.222.55.1,[ZXX: Error message] some text (_xxx.c:833)
Error for: 198.243.55.25,[ZXX: Error message] some text (_xxx.c:833)
Unexpected error for: 198.245.175.52,[Errno 104] some text here

次のようにファイルを並べ替える必要があります。

Error for,111.222.55.1,[ZXX: Error message] some text (_xxx.c:833)
Error for,198.243.55.25,[ZXX: Error message] some text (_xxx.c:833)
Unexpected error for,198.245.175.52,[Errno 104] some text here

1)単語の後にスペースがあることに注意してくださいfor: 。 2)この文字は、:例のように1行に複数回現れることがあります。だから最初の項目を交換する必要があります。for:[space]

sed検索と交換を考えました。しかし、私が好きな場所に検索を制限する方法を知りませんか?

ベストアンサー1

SEDの使用:

sed -e 's/: /,/' file > newFile

Error for,111.222.55.1,[ZXX: Error message] some text (_xxx.c:833)
Error for,198.243.55.25,[ZXX: Error message] some text (_xxx.c:833)
Unexpected error for,198.245.175.52,[Errno 104] some text here
  • デフォルトでは、sed最初の項目が置き換えられます。

おすすめ記事