Search This Blog

Monday, August 30, 2010

Cut command

cut command

Description:
Usage :
1)Cut command is mostly use to display columns from a file.
for example if a file contains a row column structure you can display
a column of file specifying column number (Starting with one for First column) using cut command.
2)It can also be used to display a given number of characters from each line of file.

Lets create a sample file (cut_sample.txt) for use.

Student Subject MarksObtained
Jack Spanish 80
Jill English 20
Krishna Gujrathi 30

Note:
our sample file contains 3 columns namely (Student,Subject,MarksObtained)
Each field is seperated by " "(a space).

1) cut command to display columns from a file.
-d is used to specify a delimeter(here the delimeter is a character that seperates
one field from other).A delimeter in above case is a space.
-f is used to specify the field name to be displayed.

Example 1:

# cut -d" " -f 1 cut_sample.txt
Student
Jack
Jill
Krishna

Example 2:

# cut -d" " -f 1,2 cut_sample.txt
Student Subject
Jack Spanish
Jill English
Krishna Gujrathi


2) cut command to display a given number of characters from each line of file.


# cut -c 1-8 cut_sample.txt
Student
Jack Spa
Jill Eng
Krishna

-c is used to specify characters in range 1-8.
In above example 8 character from each lines are displayed from file starting from 1st character .

Use following cut command to display the name of users in the system.
cut -d: -f1 /etc/passwd

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

No comments:

Post a Comment