Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

output the 2nd column of a file

Tags:

linux

given a file with two columns, separatedly by standard white space

a b
c d
f g
  h

how do I output the second column

like image 752
user678070 Avatar asked May 12 '11 22:05

user678070


People also ask

How do I print a second column in Unix?

We can also print multiple columns and insert our custom string in between columns. For example, to print the permission and filename of each file in the current directory, use the following set of commands: $ ls -l | awk '{ print $1 " : " $8 }' -rw-r--r-- : delimited_data. txt ...

How would you get just the second field of a .CSV file?

You can use the command awk .

Which command is right for display for 2 field of the file?

join command is used to join the two files based on a key field present in both the files. The input file can be separated by white space or any delimiter.


3 Answers

  1. cut -d' ' -f2
  2. awk '{print $2}'
like image 151
Roman Avatar answered Sep 29 '22 07:09

Roman


Because the last line of your example data has no first column you'll have to parse it as fixed width columns:

awk 'BEGIN {FIELDWIDTHS = "2 1"} {print $2}'
like image 44
James C Avatar answered Sep 29 '22 07:09

James C


Use cut with byte offsets:

cut -b 3

Use sed to remove trailing columns:

sed s/..//
like image 38
ninjalj Avatar answered Sep 29 '22 08:09

ninjalj