3

I have the below single long line in a file:

#cat file
5LkoVZg0BABCBkD9OieAflA==|list1|true|0|0|0|172661|1.16|186042, 5o0QEZg0BABCA3j9OieAflA==|list2|false|0|0|0|0|0|0, 5F3NnwwoBABC1Dfrsf9xucw==|list3|false|0|0|0|0|0|0, 5FaJmwwoBABC0-frsf9xucw==|...

I would like to replace the comma character with a new line, so the output looks like this:

 5LkoVZg0BABCBkD9OieAflA==|list1|true|0|0|0|172661|1.16|186042
 5o0QEZg0BABCA3j9OieAflA==|list2|false|0|0|0|0|0|
 5F3NnwwoBABC1Dfrsf9xucw==|list3|false|0|0|0|0|0|0
 5FaJmwwoBABC0-frsf9xucw==|...

More specifically the comma positioned before the " 5" character, note there is a space always before the "5" character.

I can replace the comma simply using:

#cat file | sed -e $'s/,/\\\n/g'

However this is not good enough because the comma can be located sometimes also in other awkward positions like below:

#cat file
5LkoVZg0BABCBkD9OieAflA==|list1 , |true|0|0|0|172661|1.16|186042, 5o0QEZg0BABCA3j9OieAflA==...

I prefer using sed but other solutions would be great too. I have searched internet four couple of hours but could not find a solution for my exact problem.

  • similar, although not quite what is my requirement: unix.stackexchange.com/questions/363940/… – Kalfa May 11 '17 at 9:29
  • 1
    if it's always , 5 (comma,space,five) then replace just that: s/, 5/\n 5/g ? – ilkkachu May 11 '17 at 9:42
  • @ilkkachu, thx that works too. However using solution in answer is more robust and really captures my intention to use \S*== (5LkoVZg0BABCBkD9OieAflA==) as leading sequence for a new line. – Kalfa May 11 '17 at 9:55
  • Please note that \n in the replacement string will not work with some implementations of sed, as the beheviour is not POSIX specified. – Philippos May 12 '17 at 15:13
4

sed approach:

cat file
5LkoVZg0BABCBkD9OieAflA==|list1 , |true|0|0|0|172661|1.16|186042, 5o0QEZg0BABCA3j9OieAflA==|list2|false|0|0|0|0|0|0, 5F3NnwwoBABC1Dfrsf9xucw==|list3|false|0|0|0|0|0|0, 5FaJmwwoBABC0-frsf9xucw==|...

sed 's/, \(\S*==\)/\n\1/g' file

The output:

5LkoVZg0BABCBkD9OieAflA==|list1 , |true|0|0|0|172661|1.16|186042
5o0QEZg0BABCA3j9OieAflA==|list2|false|0|0|0|0|0|0
5F3NnwwoBABC1Dfrsf9xucw==|list3|false|0|0|0|0|0|0
5FaJmwwoBABC0-frsf9xucw==|...

\S*== - sequence of non-whitespace characters followed by ==, treated as leading sequence of each line

  • Thanks a bunch! This works great, as soon as I build my rep will vote. – Kalfa May 11 '17 at 9:44
  • @Kalfa, you have my upvote for your question – RomanPerekhrest May 11 '17 at 9:45
0

A more simple solution by using tr as following:

cat file | tr "," "\n"

Where tr will look for comma , and replace it by new line \n

Output:

5LkoVZg0BABCBkD9OieAflA==|list1|true|0|0|0|172661|1.16|186042
 5o0QEZg0BABCA3j9OieAflA==|list2|false|0|0|0|0|0|0
 5F3NnwwoBABC1Dfrsf9xucw==|list3|false|0|0|0|0|0|0
 5FaJmwwoBABC0-frsf9xucw==|...

Noting that the spaces before the comma were kept.

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.