Frage

I want to add a line break (\n) in front of the 5th element on every other line:

2, 0, 0, 0, 2
4, 0, 0, 0, 4
6, 0, 0, 0, 6
8, 0, 0, 0, 8

... in order to get:

2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8

What I have so far in gawk doesn't work:

gawk '{if (NR % 2) {$5=\n$5; print} else print}'
War es hilfreich?

Lösung

You could say:

awk '{NR%2 || $5="\\n"$5 }1' filename

Note that you'll need to escape the \ in order to get a literal \.

For your input, it'd produce:

2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8

Alternatively, (as pointed out by @WilliamPursell), you could say:

awk '!(NR%2) {$5="\\n"$5 }1' filename
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top