Tuesday, September 2, 2014

Unix_Cut

The cut command is used to used to display selected columns or fields from each line of a file. Cut command works in two modes:
  • Delimited selection: The fields in the line are delimited by a single character like blank,comma etc.
  • Range selection: Each field starts with certain fixed offset defined as range.
1. Write a command to display the third and fourth character from each line of a file?
cut -c 3,4 filename
2. Write a command to display the characters from 10 to 20 from each line of a file?
cut -c 10-20 filename
3. Write a command to display the first 10 characters from each line of a file?
cut -c -10 filename
4. Write a comamnd to display from the 10th character to the end of the line?
cut -c 10- filename
5. The fields in each line are delimited by comma. Write a command to display third field from each line of a file?
cut -d’,’ -f2 filename
6. Write a command to print the fields from 10 to 20 from each line of a file?
cut -d’,’ -f10-20 filename
7. Write a command to print the first 5 fields from each line?
cut -d’,’ -f-5 filename
8. Write a command to print the fields from 10th to the end of the line?
cut -d’,’ -f10- filename
9. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is used to supress these kind of lines?
The -s option is used to supress the lines that do not contain the delimiter.
10. Write a cut command to extract the username from ‘who am i’ comamnd?
who am i | cut -f1 -d’ ‘

No comments: