GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Operating System  >  Shell Scripting
Go To First  |  Previous Question  |  Next Question 
 Shell Scripting  |  Question 25 of 47    Print  
What is use of "cut" command ?Give some examples. Can we use "awk" or "sed" instead of "cut" ? If yes then give some examples?

  
Total Answers and Comments: 7 Last Update: February 04, 2009     Asked by: Max 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 10, 2006 02:12:42   #1  
waheedha        

RE: What is use of "cut" command ???give some ex...
This utility is use to cut out columns from a table or fields from each line of a file.

cut can be used as a filter.



Either the -b -c or -f option must be specified.



-b list

The list following -b specifies byte positions (for instance -b1-72 would pass the first 72 bytes of each line). When -b and -n are used together list is adjusted so that no multi-byte character is split.



-c list

The list following -c specifies character positions (for instance -c1-72 would pass the first 72 characters of each line).



-d delim

The character following -d is the field delimiter (-f option only). Default is tab. Space or other characters with special meaning to the shell must be quoted. delim can be a multi-byte character.

 
Is this answer useful? Yes | No
December 07, 2006 06:25:38   #2  
sharifhere Member Since: December 2006   Contribution: 50    

RE: 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 | -> Deleimter used in input text to seperate columns

-f1 -> Field/Column number

while processing Huge input files Cut's perfomance is far better than awk


 
Is this answer useful? Yes | No
January 02, 2007 00:21:34   #3  
Sivaram        

RE: What is use of "cut" command ?Give some examples. ...

Cut is a powerful command in Unix Cutting fields we can use the command for example we would like to extraxct first 3 col

cut -c1-3 sample (Sample is a file name)

To extract first and 3rd col

cut -d;-f1 -f3 sample


 
Is this answer useful? Yes | No
March 25, 2007 19:59:05   #4  
cmanne Member Since: January 2006   Contribution: 49    

RE: What is use of "cut" command ?Give some examples. ...
1. CUT is used to get the specific portion(column) of information from a files separated by a specific charecter(ex: CSV file)

ex: more /etc/passwd|cut -d":" -f1
The above command is used to list the user accounts on the server.
2. AWK
ex: ls -al|awk '{print $9}'
The above command is used to get the list of filenames in the current directory. Here the fields are need not be seperated by a specific charecter.

3. SED is similar to GREP command.

 
Is this answer useful? Yes | No
March 28, 2007 07:25:35   #5  
Rahul        

RE: What is use of "cut" command ?Give some examples. ...

The cut command cuts bytes characters or fields from each line of a file and writes these bytes characters or fields to standard output. If you do not specify the File parameter the cut command reads standard input.

To display fields using a blank separated list enter:

cut -f "1 2 3" -d : /etc/passwd

The cut command produces:

su:*:0
daemon:*:1
bin:*:2
sys:*:3
adm:*:4
pierre:*:200
joan:*:202


 
Is this answer useful? Yes | No
December 22, 2007 01:11:51   #6  
prasul Member Since: December 2007   Contribution: 6    

RE: What is use of "cut" command ?Give some examples. Can we use "awk" or "sed" instead of "cut" ? If yes then give some examples?
SED is a simple editor which will be editing files without a gui and to provide operations on the command line.sed -e s/word1/word2/g filenamehttp://en.wikipedia.org/wiki/SedAWK is a command line argument used for printing out values and arguments.awk '{print $1}';http://en.wikipedia.org/wiki/awk
 
Is this answer useful? Yes | No
February 04, 2009 01:31:57   #7  
ajit_saley Member Since: February 2009   Contribution: 5    

RE: What is use of "cut" command ?Give some examples. Can we use "awk" or "sed" instead of "cut" ? If yes then give some examples?
cut -d":" -f1 3 file
Delimitor ":"
Fields/Columns 1 and 3 are printed.

In this example awk can be used. sed can not be used.

awk can do all the jobs of cut. cut can give o/p byte wise which is not possible in awk.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape