Help with awk - printing everything EXCEPT the first column?
I have a tool that we'll call "junk", which prints out a line per file, each line starting with junk that I'm not interested in (in a single column), followed by the name of the file, which I am interested in. For example:
$ junk
junk SomeFile.txt
crap SomeOtherFile.txt
stuff AThirdFile.txt
I can usually get what I'm interested in via a simple pipe to awk:
$ junk | awk '{print $2}'
SomeFile.txt
SomeOtherFile.txt
AThirdFile.txt
But consider the following:
$ junk
junk SomeFile.txt
crap SomeOtherFile.txt
stuff AThirdFile.txt
rubbish A File Containing Spaces In Its Name.txt
$ junk | awk '{print $2}'
SomeFile.txt
SomeOtherFile.txt
AThirdFile.txt
A
Now, of course I can do something like the following:
$ junk | awk '{print $2, $3, $4, $5, $6, $7, $8}'
But that has (at least) the following problems:
- It doesn't correctly handle files with multiple consecutive spaces in the filenames
- Nor filenames that start or end with spaces
- I have to make sure that I typed in enough $x variables to fit the filenames in question
- It's annoying to type
Surely there must be a better way. What is it?
To be clear, I am not wed to awk. If there's another tool that does what I want, great. And if there's an easy way to get
close to what I want (for example, forgetting about the possibility of spaces at the start and end of a filename), that's good too.
Thanks.
junk | awk '{ print substr($0, index($0, " ")); }'
Less ugly:
junk | cut -d' ' -f2-
posted by sergent at 5:03 PM on December 1, 2007 [1 favorite]