[英]How can I print a specific field from a specific line in a delimited type file
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
这是我的输入文件: 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 ). 我想提取27092011084688
( 第4行 , 第3列的值 )。
I used awk 'NR==4'
but it gave me whole 4th line. 我使用awk 'NR==4'
但它给了我整整4行。
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
. 使用,
作为字段分隔符,取记录号4并在somefile.csv
打印字段3。
$ sed -n "4p" somefile.csv | cut -d, -f3
Edit 编辑
What's this? 这是什么?
-n
turns of normal output -n
转正常输出 4p
prints the 4th row 4p
打印第4行 -d,
makes cut
use ,
as delimiter -d,
cut
use ,
as delimiter -f3
makes cut
print the 3rd field -f3
使cut
打印成第3个字段 使用awk
一种方法:
awk -F, 'NR==4 { print $3 }' file.txt
使用以下内容:
awk -F ',' 'NR==4 {print $3}'
perl
替代csv文件中第4行的print element 3:
perl -F, -lane 'print $F[2] if $. == 4' somefile.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.