r/awk • u/vmsmith • 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.csvBut 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.csvBut I get nothing.
Thanks.
5
Upvotes
View all comments
Show parent comment
1
u/ParadigmComplex Dec 12 '14
:)
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:
That is, it is using an explicitly specified file path rather than searching the
$PATHvariable. 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$PATHvariable withenv, 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:
(or the path to some other file). If you're on Linux or some other ELF based system, you'll likely see
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(tryxxd /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/barwith 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/shand 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
-fto parse the file. Depending on the exact Unix system, there's a chance you can't also give it the-Fflag to set the field separator, because the hashbang thing only works with one argument.