Quick list of Linux text processing commands

A quick list of all linux text processing commands with basic usage examples, used in scripting as well as on command line for faster operations.

  • awk

Print a text file:

awk '{ print }' /etc/passwd OR awk '{ print $0 }' /etc/passwd

Print first column of a text field with separator ‘:’

 awk -F':' '{ print $1 }' /etc/passwd

More on awk here.

  • sed – Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file. More on sed here.
 sed 's/unix/linux/' file.txt
  • cut

To extract second column from file using cut.

cut -c2 file.txt

Command to display  first field of each line from /etc/passwd file using field delimiter  ‘: ‘

cut -d':' -f1 /etc/passwd

More on cut here.

  • sort – used to sort lines in a file

This command sorts lines of a file.

sort file.txt

To reverse order of sorting use below -r option

sort -r file.txt

-n option is used to sort file content numerically as below

sort -n file.txt

More on sort here

  • uniq- command to remove duplicate lines from file
uniq file.txt

More on uniq here

  • cat – to display all contents of a file in stdout

Will display all contents of a file in stdout.

cat /etc/passwd

More on cat here

  • tr – this command does not accept filenames as input arguments instead it  accepts inputs via stdin.
tr set1 [set2]

Translate(tr) command at least one argument & max two arguments. set1,  contains list of characters in text to be replaced or removed. set2, has characters that are to be substituted for the characters listed in the first argument. More on tr here

  • grep – Used to fetch a specific string or pattern from file.

Command to fetch string from file.txt

grep -i 'helloworld' file.txt

Searching all directories for a string recursively, searches for all files under /home/username for string ‘hello’

grep -r 'hello' /home/username

Read about more usages on grep here

  • tail – lists ending lines (by default displays 10 lines) of file. Used for continous monitoring of log files.

To display last 10 lines of a file.

tail file.log

tail command more help here

  • head – list beginning of (by default displays 10 lines) file

To display first ten lines of a file.

head file.log

Read about usages on head here

  • less – command used to view a file or log interactively
less file.log

Start typing string you need to search in file,using forward slash / and string. use ‘n’ for next match of string in file, use ‘N’ to go back to previous occurence of string.

Refer this link for more examples on less command

  • more – command to display file more and navigate through it
more file.log

MORE commands to come stay connected, keep visiting.
This entry was posted in linux. Bookmark the permalink.