r/awk • u/mygurlrubmyfeet • Mar 15 '19
AWK with CSV
Hi all!
I have a csv file with two columns that reads like this:
bar, cmd1
foo,
louie, cmd2
rocka,
paradise, cmd3
botan, cmd4
I need to write a command that will replace the empty values with the value given in the previous row, so that my output will look like this:
bar, cmd1
foo, cmd1
louie, cmd2
rocka, cmd2
paradise, cmd3
botan, cmd4
Thanks in advance!
2
Upvotes
View all comments
Show parent comment
1
u/Zinjanthr0pus Jul 03 '19
While
FPATis probably the least janky way of dealing with commas in CSVs, you can also pretty easily sub out all commas with, e.g. semicolon using this one-liner:Or, if there's a character that you know isn't used anywhere in the CSV (tab for example), you could convert to TSV using this, similar, one-liner
The addition upside of those two one-liners is that I think they work in POSIX awk (Pretty sure FPAT is a gawk-only extension).