CUT


This command comes in handy when you need to extract (that is, "cut out") various fields of data from a data file or the output of a command. The general format of the cut command is
cut -cchars file

where chars specifies what characters you want to extract from each line of file. This can consist of a single number, as in -c5 to extract character 5; a comma-separated list of numbers, as in -c1,13,50 to extract characters 1, 13, and 50; or a dash-separated range of numbers, as in -c20-50 to extract characters 20 through 50, inclusive. To extract characters to the end of the line, you can omit the second number of the range; so 
cut -c5- data
extracts characters 5 through the end of the line from each line of data and writes the results to standard output.
[root@server1 ~]# cat file9 The Unix operating system was pioneered by Ken Thompson and Dennis Ritchie at Bell Laboratories in the late 1960s. One of the primary goals in the design of the Unix system was to create an environment that promoted efficient program development.
[root@server1 ~]# cut -c5- file9 Unix operating system was pioneered by Ken pson and Dennis Ritchie at Bell Laboratories he late 1960s. One of the primary goals in design of the Unix system was to create an ronment that promoted efficient program lopment.
[root@server1 ~]# cut -c5 file9 U p h d r l
$ who | cut -c1-8 Extract the first 8 characters ashish ashu mahajan
[root@server1 ~]# who |cut -c1-8,18- root 2011-04-13 14:46 (192.168.1.2) root 2011-04-13 15:25 (192.168.1.2) ashiashi 2011-04-13 15:26 (192.168.1.2) ashi 2011-04-13 15:27 (192.168.1.2)

The -d and -f options are used with cut when you have data that is delimited by a particular character. The format of the cut command in this case becomes
cut -ddchar –ffields file

where dchar is the character that delimits each field of the data, and fields specifies the fields to be extracted from file. Field numbers start at 1, and the same type of formats can be used to specify field numbers as was used to specify character positions before (for example, -f1,2,8, -f1-3, -f4-).
So to extract the names of all users of your system from /etc/passwd, you could type the following:
$ cut -d: -f1 /etc/passwd     Extract field 1

root

cron

bin

uucp

ash

ashiashi

other

$ cut -d: -f1,6 /etc/passwd >>>Extract fields 1 and 6 root:/ cron:/ bin:/ uucp:/usr/spool/uucp asg:/ ashiashi:/users/ashiashi other:/

If the cut command is used to extract fields from a file and the -d option is not supplied, cut uses the tab character as the default field delimiter.
If you have a string “one two three”, which shell command would you use to extract the strings?
echo $string | cut -d” ” -f1 echo $string | cut -d” ” -f2 echo $string | cut -d” ” -f3 
How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20
or
cut -c10-20
 
 
What is use of “cut” command? Give some examples.
Cut – Utility used to cut/Strip out the required data/text from the source.
Cut can be used in three modes,
  Stripping by Character
     cut -c 1-3
  Striping by Byte length
     cut -b -1-72
Stripping by delimiter and fields.
     cut -d “|” -f1
Where   
-d “|” -> Delimiter used in input text to separate columns
-f1 -> Field/Column number  
While processing huge input files, Cut’s performance is far better than awk
 
 

Comments

Popular posts from this blog

Standard Error

Shell Basic Operators.

AWK