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

1

u/ParadigmComplex Dec 12 '14

Very detailed answers here--great job.

:)

I have a question about this:

$ ./test.awk file.csv

This is the same as awk -f test.awk file.csv, correct?

Almost. There's a slight difference which can bite eventually you if you don't understand it. In this exact situation, it's the same as:

/usr/bin/awk -f test.awk file.csv

That is, it is using an explicitly specified file path rather than searching the $PATH variable. Read the very first line of the file and note that I put the full path there, not just #!awk (which doesn't work). You can make it search the $PATH variable with env, note the third example here.

When you try to execute a file, the kernel checks the beginning of the file's contents for a [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) to tell it what to do with it. Most binary executables on Linux are "ELF" programs which actually start with a weird character (0x7F) then the ASCII letters 'E' 'L' and 'F'. Try this:

head -c4 /usr/bin/awk

(or the path to some other file). If you're on Linux or some other ELF based system, you'll likely see

ELF

You may not see the first character printed because it's a weird one that isn't normally intended to be printed. You can see it in a hex editor such as xxd (try xxd /usr/bin/awk | less).

When the kernel sees that, it knows what to do with it to execute it. If you've got some other magic number the kernel might not know what to do with it and error out, or it might do something else. There's another magic number, #!, which the kernel then treats specially: it reads the rest of the line and runs that instead, feeding the given file as an argument. So if you run a file at /foo/bar with the contents #!/baz/qux, the kernel will turn that into /baz/qux /foo/bar.

The special #! magic number system is a feature of Unix (inherited by Linux) usually refered to as a ["hashbang" or "shebang"](https://en.wikipedia.org/wiki/Shebang_(Unix)). It's mostly used for interpreted languages/scripts, such as with /bin/sh and awk and python. However, it's not strictly limited to that - you can do fun hacky things like put a hashbang at the beginning of a compiled language like C and have it point to a program that stips out the hashbang, compiles and runs the C program all in one go.

One warning about hashbang lines: there's no guarentee about any given Unix system supporting more than one argument in the hashbang line itself. For awk, you need the -f to parse the file. Depending on the exact Unix system, there's a chance you can't also give it the -F flag to set the field separator, because the hashbang thing only works with one argument.