Linux – How to match a pattern and substitute only part of the pattern

linuxsedtext processing

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.

Best Answer

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

Related Question