Blog Archive

Monday, February 21, 2011

how to display the specified colunmn in unix / linux : Unix cut command

http://www.softpanorama.org/Tools/cut.shtml

EX;


cut -d ' ' -f 2-7
retrieves the second to seventh field assuming that each field is separated by a single (note: single) blank.  Fields are counted starting from one.

Here is example on how to print first and the second from the last columns:
 perl -lane 'print "$F[0]:$F[-2]\n"'
Here's a more complex one-line script that will print out the fourth word of every line, but also skip any line beginning with a # because it's a comment line.
perl -naF 'next if /^#/; print "$F[3]\n"'

cut -c 4,5,20 foo # cuts foo at columns 4, 5, and 20.
cut -c 1-5 a.dat | more  # print the first 5 characters of every line in the file a.dat
cut -c -5 a.dat | more  #  same as above but using open range

cut -d ":" -f1,7 /etc/passwd  # cuts fields 1 and 7 from /etc/passwd
cut -d ":" -f 1,6- /etc/passwd # cuts fields 1, 6 to the end from /etc/passwd

No comments:

Post a Comment