How can I print a specific field from a specific l

2019-06-20 08:16发布

I have a sorted, delimited type file and I want to extract a specific field in specific line.

This is my input file: somefile.csv

efevfe,132143,27092011080210,howdy,hoodie
adfasdfs,14321,27092011081847,howdy,hoodie
gerg,7659876,27092011084604,howdy,hoodie
asdjkfhlsdf,7690876,27092011084688,howdy,hoodie
alfhlskjhdf,6548,27092011092413,howdy,hoodie
gerg,769,27092011092415,howdy,hoodie
badfa,124314,27092011092416,howdy,hoodie
gfevgreg,1213421,27092011155906,howdy,hoodie

I want to extract 27092011084688 (value from 4th line, 3rd column).

I used awk 'NR==4' but it gave me whole 4th line.

5条回答
太酷不给撩
2楼-- · 2019-06-20 08:26

One way using awk:

awk -F, 'NR==4 { print $3 }' file.txt
查看更多
欢心
3楼-- · 2019-06-20 08:27
$ sed -n "4p" somefile.csv | cut -d, -f3

Edit

What's this?

  • -n turns of normal output
  • 4p prints the 4th row
  • -d, makes cut use , as delimiter
  • -f3 makes cut print the 3rd field
查看更多
Explosion°爆炸
4楼-- · 2019-06-20 08:33

Fairly straightforward:

awk -F',' 'NR == 4 { print $3 }' somefile.csv

Using , as a field separator, take record number 4 and print field 3 in somefile.csv.

查看更多
干净又极端
5楼-- · 2019-06-20 08:39

Use the following:

awk -F ',' 'NR==4 {print $3}'
查看更多
Root(大扎)
6楼-- · 2019-06-20 08:44

perl alternative to print element 3 on line 4 in a csv file:

perl -F, -lane 'print $F[2] if $. == 4' somefile.csv
查看更多
登录 后发表回答