CAT, GREP,SORT, UNIQ Cheat Sheet - abhishekkhare/UNIX GitHub Wiki

CAT - Concatenate and print the content of files.

  • cat fileName -> Display the content of the file.
  • cat *.txt -> display content of all txt files.
  • cat sample.txt sample1.txt > sample2.txt -> concatenate two files.
  • variable_content = 'cat sample.txt' -> To put content of a file in a variable.
  • cat -n fileName -> Showing line numbers
  • cat -v fileName -> shows all non-printing characters, except for line feed and tab
  • cat fileName|more -> file having large number of content that wonโ€™t fit in output terminal and screen scrolls up very fast, we can use parameters more.
  • cat -e fileName -> -e option that โ€˜$โ€˜ is shows at the end of line

GREP - Globally Search a Regular Expression and Print

  • grep "expression" fileName -> returns all the lines that contain a string matching the expression in the file "file".
  • grep ab * -> find ab in all files in the current directory.
  • grep -i "expression" fileName -> returns all the lines that contain a string matching the expression(Case Insensitive) in the file "file".
  • grep -w "expression" * -> full word search
  • grep -A "expression" * -> Show N lines after the match
  • grep -B "expression" * -> Show N lines before the match
  • grep -C "expression" * -> Show N lines around the match
  • grep -R "expression" * - > Recursively look into the sub directories.
  • grep -v "expression" * - > invert-match - Selected lines are those not matching any of the specified patterns.
  • grep -c "expression" fileName -> Only a count of selected lines is written to standard output.

SORT

  • sort filename -> sorts the content of the file(line by line) and prints it on stdout.
  • sort -r filename -> sorts the content of the file(line by line) in reverse order and prints it on stdout.
  • ls -l|sort -nk5 -> sort the file listing by the size. (n - number, k - is key and 5 - is the column number).
  • sort -u fileName -> sort the file but show only the distinct.
  • sort fileName1 fileName2 -> combined two file and then sort the result and print it.
  • sort -f fileName -> sort but ignore cases.
  • sort numbers.txt -> would be sorted in alphabetical order.
  • sort -n numbers.txt -> would be sorted in numerical order.

UNIQ