1

I have comma separated file with two columns where the first column is always empty and the second one is sometimes empty (when the last column is empty there is no final comma):

,value_c1_1
,,value_c2_1
,,value_c2_2
,,value_c2_3
,value_c1_2

I would like to use awk to fill empty column value with previous non-empty column value and then get rid of the rows where the second column is empty:

,value_c1_1,value_c2_1
,value_c1_1,value_c2_2
,value_c1_1,value_c2_3

The big difference with the answer to this question

awk '/^ /{$0=(x)substr($0,length(x)+1)}{x=$1}1' file

is that the fields are character separated (instead of being of fixed length) and that the first column is always empty.

user189035
  • 5,589
  • 13
  • 52
  • 112

1 Answers1

2
awk -F, 'BEGIN { OFS = FS } { if ($2 == "") $2 = last2; else last2 = $2; print }'

If column 2 is empty, replace it with the saved value; otherwise, save the value that's in column 2 for future use. Print the line. (The BEGIN block ensures the output field separator OFS is the same as the (input) field separator, FS.)

If you only want to print lines with 3 fields, then:

awk -F, 'BEGIN { OFS = FS }
         { if ($2 == "") $2 = last2; else last2 = $2; if (NF == 3) print }'
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @quick question: do you think this could be done (faster) with `grep`? – user189035 Jun 19 '18 at 15:19
  • 1
    I can't think of a way to do it with `grep` — `grep` selects and does not replace. Awk seems to me to be the right tool for the job. It could also be done in Perl or Python (or …), and it probably could be done with `sed` but it would be painful and obscure whereas it is simple and clear in Awk. – Jonathan Leffler Jun 19 '18 at 15:20
  • thanks for the prompt answer (I just wanted to check I did not force a wrong headed constraint on the problem by asking for awk only answers!) – user189035 Jun 19 '18 at 15:21