I've got a header in a text file that looks like this:
First-Name:Last-Name:City:Home-Phone:Cell-Phone
What I need to do is display the Last-Name
and the Cell-Phone
number of each entry.
I'm new to bash scripting, and I'm not really sure what I can use to do this. I was told awk
could be useful, but after looking around, I still don't quite understand how to use it.
用-F
指定字段分隔符:
awk -F":" '{print $2, $5}'
I've found csvkit to be a useful utility for dealing with files like this. It's a lot more intuitive than the low-level unix utilities, though admittedly less powerful.
Take the following file:
First-Name:Last-Name:City:Home-Phone:Cell-Phone
Alice:Ashbury:Boston:111-111-1111:444-444-4444
Bob:Brown:Boston:222-222-2222:555-555-5555
Carol:Chaplin:Chicago:333-333-3333:666-666-6666
You can extract the second and fifth columns using csvcut
:
csvcut test.csv -d ':' -c 2,5
Which gives the following output (including the header row):
Last-Name,Cell-Phone
Ashbury,444-444-4444
Brown,555-555-5555
Chaplin,666-666-6666
Combining the various csvkit commands together lets you quickly explore your data. For example, the following will filter to all lines with Boston
as the City
:
csvformat test.csv -d ':' | csvgrep -m Boston -c 3 | csvcut -c 2,5 | csvlook
And csvlook
gives you pretty-printed output:
|------------+---------------|
| Last-Name | Cell-Phone |
|------------+---------------|
| Ashbury | 444-444-4444 |
| Brown | 555-555-5555 |
|------------+---------------|
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.