Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some problem with bash script.

I've got a string which has some repeated patterns like this.

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 ...

Each fields is separated by tab key.

I want it to look like this...

1 2 3 4
1 2 3 4
1 2 3 4
...

How can I solve this problem using bash script like cut, sed, awk ... ?

It looks very easy but so difficult to me...
Posted

1 solution

You can use sed like this:

Solution:
s='1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4'
p='1 2 3 4'
echo "$s"|sed "s/$p\s*/&\n/g"
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4


You can use infinite while loop and use the input file and iterate through each line to use this logic for murtiple lines.

Live Demo
http://ideone.com/P59OCJ[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900