Add '\n' after a specific number of delimi

2019-05-25 21:26发布

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

标签: bash csv sed awk cut
4条回答
太酷不给撩
2楼-- · 2019-05-25 21:51

Using (GNU) sed:

... | sed -r 's/([^;]*;){4}/&\n/g'

[^;]*; 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 make sed replace all matches in each input line instead of just the first match per line.

查看更多
Deceive 欺骗
3楼-- · 2019-05-25 21:55

This might work for you (GNU sed):

sed 's/;/\n/4;/./P;D' file
查看更多
狗以群分
4楼-- · 2019-05-25 22:02

Perl solution:

perl -pe 's/;/++$i % 4 ? ";" : "\n"/ge; chomp'

Only works if the number of fields is divisible by four.

查看更多
淡お忘
5楼-- · 2019-05-25 22:03

Read each line into an array, then print 4 groups at a time with printf until the line is exhausted.

while IFS=';' read -a line; do
    printf '%s;%s;%s;%s\n' "${line[@]}"
done < input.txt
查看更多
登录 后发表回答