Text processing in Linux can be done using sed, cut, grep commands and with vi editor. We can find and replace a string, find and delete string etc.

Finding IP Addresses from Apache Log file through cut command

cut -d ‘-’ -f1   /var/log/httpd/access_log
  • The -d ‘-’ refers to delimeter –
  • The -f1 is for first field in Apache log file

 

Remove particular IP Addresses from Apache log through sed command

sed -ie ‘/^10\.0\./d’ /var/log/httpd/access_log
  • The -ie refers to inline edit in file
  • The ‘/^10 indicates that IP must start with 10
  • The \ indicates that special character . is text
  • The /d indicates that delete this line

 

Remove lines containing a key word through sed

sed -i  ’1,30{/index/d}’ /var/log/httpd/access_log
  • The 1,30 refers to first 30 lines
  • The {/index/d} indicates delete lines containing word “index”

 

Remove blank lines from a text file using vi editor

:g/^$/ d
  • The g refers globally
  • The ^$/ d indicates delete blank lines

 

Remove special character # from a text file using vi editor

:1,$s/\#//g
  • The $s/ refers search from start
  • The \# indicates take # as character
  • The //g replace it with nothing i.e remove character