How can I add a \n
after each four ;
delimiter in a CSV file (with bash)?
Input file sample:
aaaa;bbbbbb;cccc;ddddd;eeee;ffff;gggg;hhhh;iii;jjjj;kkkk;llll;
Output needed :
aaaa;bbbbbb;cccc;ddddd
eeee;ffff;gggg;hhhh
iii;jjjj;kkkk;llll
How can I add a \n
after each four ;
delimiter in a CSV file (with bash)?
Input file sample:
aaaa;bbbbbb;cccc;ddddd;eeee;ffff;gggg;hhhh;iii;jjjj;kkkk;llll;
Output needed :
aaaa;bbbbbb;cccc;ddddd
eeee;ffff;gggg;hhhh
iii;jjjj;kkkk;llll
Using (GNU)
sed
:[^;]*;
matches a sequence of characters that are not semicolons followed by a semicolon.(...){4}
matches 4 times the expression inside the parentheses.&
in the replacement is the whole match that was found.\n
is a newline character.The modifier
g
makesed
replace all matches in each input line instead of just the first match per line.This might work for you (GNU sed):
Perl solution:
Only works if the number of fields is divisible by four.
Read each line into an array, then print 4 groups at a time with
printf
until the line is exhausted.