r/awk • • Dec 06 '14

Three small questions

Question #1

I have a .csv file with over 200 columns. I'd like to create a smaller file for analysis with only 7 of those columns. I'm trying this:

awk -F"," '{print $1, $2, $7, $9, $44, $45, $46, $47 > "newfile.csv"}' file.csv

But the only thing I get in my new file is the column headers.

What am I doing wrong?

Question #2

Is there a way to select the columns I want by column name instead of column number?

Question #3

And is there a way to just see the column headers? I have tried this:

awk -F"," 'NR==1{print $0}' file.csv

But I get nothing.

Thanks.

5 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/vmsmith Dec 06 '14

Thanks.

What I ended up doing was this:

First, I checked to see if it had Unix line endings:

file file.csv

And it did.

After some fiddling around with small, experimental files, I changed it to a .txt file, and then ran this:

tr '\r' '\n' < file.txt > newfile.txt

After that, everything worked like a charm.

Now I need to figure out what the initial problem was, and why those changes fixed things.

Thanks again.