0

I have a long file that I need to reprocess for entering it into database. The file's data are in this format:

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

I need to re arrange the file to be like this:

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) Please note that there is a space after the word for: 2) That the character : can occur more than once in a line as you see in the example. So I need to replace the first occurrence after the for:[space]

I thought of sed to search and replace. But do not know how to limit the search for the position I want?

3

using 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
  • By default, sed replaces the 1st occurrence.
  • I want to output in a new file. Not change the same file. – user9371654 Oct 4 '18 at 19:30
  • How does it know that thee : is the first occurrence? I do not want to change every :. Can you clarify your command briefly please? – user9371654 Oct 4 '18 at 19:31
  • @user9371654 , Check my update. – msp9011 Oct 4 '18 at 19:37
1

awk solution:

 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
  • I do not want spaces before and after comma. – user9371654 Oct 4 '18 at 19:34
  • Please check the output I posted. This is not what I need. – user9371654 Oct 4 '18 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.