You can use the Linux command ʻawk` to display a specified column in a file.
[demo@centos8 ~]$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[demo@centos8 ~]$
There are the following files (b01.txt
) separated by half-width spaces.
b01.txt
1 a ab
2 bb bcd
3 ccc cdef
4 dddd defgh
5 eeeee efghij
Only the second column can be displayed with the following command.
awk '{print $2}' b01.txt
Execution result
[demo@centos8 test]$ awk '{print $2}' b01.txt
a
bb
ccc
dddd
eeeee
[demo@centos8 test]$
There are the following files (b02.txt
) separated by", ".
b02.txt
1,a,ab
2,bb,bcd
3,ccc,cdef
4,dddd,defgh
5,eeeee,efghij
Only the second column can be displayed with the following command.
awk -F',' '{print $2}' b02.txt
Execution result
[demo@centos8 test]$ awk -F',' '{print $2}' b02.txt
a
bb
ccc
dddd
eeeee
[demo@centos8 test]$
There is the following tab-delimited file (b03.txt
).
b03.txt
1 a ab
2 bb bcd
3 ccc cdef
4 dddd defgh
5 eeeee efghij
Only the second column can be displayed with the following command.
awk -F'\t' '{print $2}' b03.txt
Execution result
[demo@centos8 test]$ awk -F'\t' '{print $2}' b03.txt
a
bb
ccc
dddd
eeeee
[demo@centos8 test]$
There are the following files (b01.txt
) separated by half-width spaces.
b01.txt
1 a ab
2 bb bcd
3 ccc cdef
4 dddd defgh
5 eeeee efghij
You can display the 3rd row and 2nd column with the following command.
awk 'NR==3 {print $2}' b01.txt
Execution result
[demo@centos8 test]$ awk 'NR==3 {print $2}' b01.txt
ccc
[demo@centos8 test]$
The following command combined with the sed
command can also display the 3rd row and 2nd column.
sed -n 3p b01.txt | awk '{print $2}'
Execution result
[demo@centos8 test]$ sed -n 3p b01.txt | awk '{print $2}'
ccc
[demo@centos8 test]$
that's all
Recommended Posts