简体   繁体   中英

Using awk command in bash to extract a single field from a record

I want to use awk to extract a single field from a list of records. For example,

Assignment1:/home/dir/:Admin:08-07-12
Assignment2:/home/dir/:Paul:09-22-13

I want to extract the 1st field from the second line, or the third field from the first line. Any ideas?

awk -F: 'NR == 2 { print $1 }'
awk -F: 'NR == 1 { print $3 }'

For the given line number, print the specified field.

It doesn't work

My apologies, I am using the Bourne shell and this does not work.

As noted in the comment, I don't believe that the Bourne shell has anything directly to do with the problem. (If $IFS is set to something odd, or some other peculiar setup applies, maybe that has an effect. But in a normal Bourne shell, there should be no problem.)

Here's what I get on my machine (an Ubuntu 14.04 derivative, but I'm confident I'd get the same result on Mac OS X 10.10.3, and pretty much any other Unix-like system, too). This is using Bash, but I don't think that's a factor — I'll go out on a limb and say that Korn shell, Zsh, Dash, and Heirloom Shell would all work the same on this; heck, it should work in the C shell family of shells too since there's nothing special about the notations used). It is using GNU awk too, but I don't think that's a factor either.

$ cat data
Assignment1:/home/dir/:Admin:08-07-12
Assignment2:/home/dir/:Paul:09-22-13
$ awk -F: 'NR == 2 { print $1 }' data
Assignment2
$ awk -F: 'NR == 1 { print $3 }' data
Admin
$

The output looks like record 2, field 1 and record 1, field 3 to me. Please find a way to demonstrate what you're doing and the result you get — probably, add something to the question. We can clean it up later when we've worked out what's going wrong with your setup. Please identify your platform reasonably clearly, too.

You can always use 'cut' from bash like so

cut -d ":" -f "1,3" < asdf
Assignment1:Admin
Assignment2:Paul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM