4

I am trying to write awk for a file I have. Example of the dataset is

S,CV0110,1235
S,1234
D,CQ120,3245
P,7894

Desired outcome is as follows (added empty field when the number of fields in a row is 2)

S,CV0110,1235
S,,1234
D,CQ120,3245
P,,7894

I tried this but it is adding value for all data rows instead of those that have 2 fields.

printf 'S,CV010,1235\r\n' | awk 'BEGIN{FS=OFS="fs"}{$n = $n OFS value}1'
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
mike
  • 69
  • 2

3 Answers3

6

Given

$ cat file
S,CV0110,1235
S,1234
D,CQ120,3245
P,7894

then

$ awk -F, 'BEGIN{OFS=FS} NF<3{$1 = $1 OFS} 1' file
S,CV0110,1235
S,,1234
D,CQ120,3245
P,,7894
steeldriver
  • 78,509
  • 12
  • 109
  • 152
4
sed '/,.*,/!s/,/,,/' file
  • /,.*,/! If line does not contain two commas,
  • s/,/,,/ Substitute the first comma by two commas.

This can be generalized to more fields. E.g., to add an empty field if some line is missing the 7th field,

sed -E '/,(.*,){5}/!s/,/,,/' file
Quasímodo
  • 18,625
  • 3
  • 35
  • 72
2

Assuming the values en each column are always similar.

If a single letter at the start of the line is followed by 4 numbers and the line ends, add a comma:

sed '/^[A-Z],[0-9]\{4\}$/s/,/,,/' file

Output:

S,CV0110,1235
S,,1234
D,CQ120,3245
P,,7894
schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57