Linux: Filter commands cut, wc, spell and sort

Linux: cut command

  • The cut linux filter command cuts off and picks up a given number of character or fields from the specified file
  • For example if you want to display only selected fields from the database of students say name and class then you can use cut command.
  • In the following example user displays the values of the fields name and class which is the 2nd and 4th field respectively in the database studentinfo

  • Example: cut –f 2, 4 studentinfo

  • If we want to view through few fields, for example 1 through 4 then we can use the following command

  • Example: cut –f 1-4 studentinfo

  • The cut command assumes that the fields are separated by a tab character. If the fields are delimited by some character other than the default tab character, cut supports an option ‘–d’ which allows us to set the delimiter

  • For example the file studentinfo may have the information for each student stored in the following format

  • Example: Rollnum:Name:age:Class:Adrress:City:Pin: Country

  • Each piece of information is separated by a colon, hence we require the field delimiter to be recognized as ‘:’.The command for listing the name and the class field is now will be cut –f 2,4 –d “:” studentinfo
  • The cut command can also cut specified columns from a file and display them on the standard output.The switch used for this purpose is ‘–c’

  • Example: cut –c 1-7 studentinfo

Linux: wc filter command

  • The linux command wc filter takes data stream as its input, which is usually data read from a file. It then counts the number of lines, words, and characters (including the newline character, found at the end of a line) in the file and outputs these counts
  • In the following example given, the wc command is used to find the number of lines, words, and characters in the file named as karan

  • Example: wc karan

Linux: spell filter command

  • The linux command spell filter checks the spelling of words in its input and outputs only the misspelled words
  • In the following example the command checks for the spelling of the words in the file karan and displays the misspelled words

  • Example: spell karan

  • Now using redirection, you can save those words in a file
  • With a pipe, you can also print them
  • In the next example, the user saves the misspelled words to a file called wrongspell

  • Example: spell karan > wrongspell

  • You can pipe the output of one filter into another filter, in effect, applying the capabilities of several filters to your data

  • For example, suppose you only want to know the number of words that are misspelled
  • You could pipe the output of the spell filter into the wc filter, which would count the number of misspelled words

  • In the next example, the words in the karan file are checked for its spelling, and the list of misspelled words is piped to the wc filter. The wc filter, with its -w option, then counts those words and outputs the count

  • Example: spell karan | wc -w

Linux: sort filter command

  • The linux sort filter outputs a sorted version of a file
  • Sort is a useful utility with many different sorting options. These options are primarily designed to operate on files arranged in a database format
  • The sort filter sorts, character by character, on a line
  • If the first character in two lines is the same, sort will sort on the next character in each line
  • You can, also save the sorted version in a file or send it to the printer
  • In the following example, the user saves the sorted output from the file karan in a file called raj

  • Example: sort karan > raj

Linux:Pattern searching command << Previous
Next>> Linux: Compressing-Decompressing, ps, kill, and su commands

Our aim is to provide information to the knowledge

comments powered by Disqus