rcwizard12435
rcwizard12435

Reputation: 63

How can I make repeating characters into a new line in a text file

I have a text file similar to this (This is how the data is extracted unfortunately):

asdf,10.vip0,sample_pool1,;;;;;;;;10.server1;;;;;;;;;;11.server2;;;;;;;;;;12.server3;;
asdf,10.vip1,sample_pool2,;;;;;;;;10.server1;;;;;;;;;;11.server2;;;;;;;;;;12.server3;;
asdf,10.vip2,sample_pool3,;;;;;;;;10.server1;;;;;;;;;;11.server2;;;;;;;;;;12.server4;;

Is there a command(s) I can use to get it formatted like this?

asdf,10.vip0,sample_pool1,10.server1
11.server2
12.server3
asdf,10.vip1,sample_pool2,10.server41
11.server42
12.server43
asdf,10.vip2,sample_pool3,10.server31
11.server32
12.server34

Upvotes: 1

Views: 41

Answers (2)

mauro
mauro

Reputation: 5940

Maybe something like this?

$ sed 's/;;;\+//; s/;;;\+/\n/g; s/;;$//' file
asdf,10.vip0,sample_pool1,10.server1
11.server2
12.server3
asdf,10.vip1,sample_pool2,10.server1
11.server2
12.server3
asdf,10.vip2,sample_pool3,10.server1
11.server2
12.server4

it removes the first group of repeated semicolon s/;;;\+// then it replaces all the remaining groups with new-line s/;;;\+/\n/g and, finally, get rid of the two semicolon at the end s/;;$//

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

awk approach:

awk -F, '{ sub(/,;+/,",",$0); sub(/;+$/,"",$0); gsub(/;{3,}/,"\n",$0) }1' file

The output:

asdf,10.vip0,sample_pool1,10.server1
11.server2
12.server3
asdf,10.vip1,sample_pool2,10.server1
11.server2
12.server3
asdf,10.vip2,sample_pool3,10.server1
11.server2
12.server4

Upvotes: 1

Related Questions