ridgedale
ridgedale

Reputation: 190

How to insert additional field after third field if number of field separators in line is 5 using sed or awk

Is it possible to run a sed command that will check the number of field separators in a line and insert an additional separator if the number of separators in the line is 5, for example?

Source data example:

a,aaa|bbbb|cccc|dddd|eeee|ffff|gggg
aaaa|bb,bb|dddd|eeee|fff,f|gggg
aaa,a|bbbb|cccc|dddd|eeee|ffff|gggg

Target output example:

a,aaa|bbbb|cccc|dddd|eeee|ffff|gggg
aaaa|bb,bb||dddd|eeee|fff,f|gggg
aaa,a|bbbb|cccc|dddd|eeee|ffff|gggg

Note: The target is to insert an additional field separator (|) immediately before or after the second field separator of the line to create a blank 3rd field, if only 5 field separators exist in the line.

If this is not possible using sed, would awk be able to accomplish the task?

Any guidance would be appreciated.

Upvotes: 1

Views: 76

Answers (3)

potong
potong

Reputation: 58401

This might work for you (GNU sed):

sed 's/|/&/6;t;s/|/&&/2' file

If the number of field separators (in this case 6) is sufficient, bail out.
Otherwise, double the field separator on the required field (in this case 2).

If you only want to add the separator if there are exactly five, use:

sed 's/|/&/6;t;s/|/&/5;T;s/|/&&/2' file

Upvotes: 1

Ruud Helderman
Ruud Helderman

Reputation: 11018

It is most certainly possible with sed:

sed '/^[^|]*\(|[^|]*\)\{5\}$/s/|/||/2'

The 5 is the number of separators that will trigger replacement, and the 2 at the end of the line is the separator count where replacement will take place.

This is already a bit more readable and a lot more maintainable than my original attempt:

sed 's/^\([^|]*|[^|]*\)\(\(|[^|]*\)\{4\}\)$/\1|\2/'

Still, the awk solution is the best in terms of readability.

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

Something like this should work:

awk -F '|' -v OFS='|' 'NF<7{$2=$2 FS} 1'

-F '|' sets the input field separator to |.
-v OFS='|' sets the output field separator to |.

When the number of fields NF is lower than 7, a field separator FS is appended to the second field.

Upvotes: 4

Related Questions