我有一个很长的文件,需要重新处理才能将其输入数据库。该文件的数据采用以下格式:
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) 该字符:
可以在一行中出现多次,如示例中所示。所以我需要替换之后的第一次出现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
替换第一次出现的位置。
答案2
awk
解决方案:
awk '{sub(/: /,",")}1' file
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