Delete specific line in awk

Hi, I'm totally new to awk, so thanks in advance for your help.

I want to make a csv file out of a text file I've exported from a database I want to do two things:

  1. Change pipes ("|") to commas (",") to make a text file into CSV
  2. Remove the second line of the file, which is useless junk.

Here's what I have so far to accomplish #1. I'm struggling on #2. Help?

function pull() {
cat $@ | awk '{gsub(/\|/,","); print;}' > $@.csv;
}

Many thanks

Hi,
maybe sed is a better reply to that something like

sed "s/|/,/g; 2d" $@ >$@.csv
1 Like

You get a useless use of cat award :wink:

You don't need gsub to handle separators in awk. awk handles separators natively, via the FS and OFS variables. Just make sure you alter the line before printing it -- even something useless like $1=$1 will do -- and it'll change all the separators for you.

Also, pretty sure that redirection won't do what you want it to -- replace in every file. You can't read and write the same file at the same time with shell redirection. You also can't redirect to several output files at once with $@.

How about this:

awk -v FS="|" -v OFS="," '{ $1=$1 } NR != 2' < input > output

...which, to overwrite the original file, you'd do:

awk -v FS="|" -v OFS="," '{ $1=$1 } NR != 2' < input > /tmp/$$
cat /tmp/$$ > input.converted

Remove the '.converted' once you've tested the code and found it does what you want -- it really stinks to lose all your originals to a bad program.

If you wanted to convert many files:

function fixfiles
{
        for FILE in "$@"
        do
                awk -v FS="|" -v OFS="," '{ $1=$1 } NR != 2' <"$FILE" > /tmp/$$
                cat /tmp/$$ > "$FILE".converted
        done
}

Many thanks
[/quote]

1 Like

Thank you, both. Very helpful. Glad you guys are on this board!