Regular Expression - Tips & Tricks


SED
  - A non-interactive line editor.  
AWK - Full-featured text processing language

S(tream) ED(itor)
      
       It receives text input, whether from stdin or from a file, performs certain operations on specified lines of the input, one line at a time, then outputs the result to stdout or to a file.

Tip#01
                   
    To print the line numbers with space in the beginning of a file as

        begin       
        start 
        end 

       1 begin       
       2 start
       3 end

    sed = filename | sed 'N;s/\n/ /g'                
  
Tip#02

    To remove the blank lines in a file.
   
    sed -e '/^$/d' < input-file
Tip#03

    To change the particular pattern excpet the first occurrence in a file

    test pattern test test
   
    test pattern change change
   
    echo test pattern test test | sed -e '/test/change/g;s/^change/test/'
Tip#04

    To print only some particular lines in a file.
   
    To print the lines 1,2,3 only.

    sed -n 1,3p             
<or>
    sed -e 1,3!d
Tip#05

    To print other than the particular lines.
 
    sed -e 1,3d             
<or>
    sed -n 1,3!p
Tip#06

    To change a pattern to other pattern whose line is containing the particular pattern
   
    hai hello hai who are you
    hai hello hai this is sed
       
    Change a lines pattern hai to bye which contains a pattern as who
    sed -e '/who/s/hai/bye/g' < inputfile
Tip#07

    To change the occurrence of a particular pattern in a line.
  
    hai hello hai who are you
    hai hello hai this is sd

    To change a line's 2 second occurrence of a pattern hai to bye
    which contains a pattern as sed in the last of the particular line

    sed -e '/sd$/s/hai/bye/2'
Tip#08

    To show the lines which contains in the begin without the symbol "#"

    sed -e '/^#/d' < input_file
Tip#09

    To put a character ";" at end of all lines

    sed -e '/$/;/' < input_file
Tip#10

    Make the new line after the character ";"

    printf("Enter A");scanf(&a);printf("A=%d',a);
   
    cat input_file | sed -e 's/;/;\      <press enter>
    /g'

   
    It will give as
    printf("Enter A");
    scanf(&a);
    printf("A=%d',a);
Tip#11

    To put the double space between lines.

    sed G filename
Tip#12

    To use the variables in the patterns of the sed command

    start hai hai sed test programme end

     export str=test;export str1=first
      
     sed -e 's/'$str'/'$str1'/'
 
A ( Aho) W ( weinberger ) K ( Kernighan )

Awk is a full-featured text processing language with a syntax reminiscent of C.
Awk breaks each line of input passed to it into fields.


Tip#01
   
    To find the process ID

    pattern - file-name or process-ID  

    ps -ae | awk '/pattern/ { print $1 }'                <or>
    ps -aef | grep -v awk | awk '/file-name/' { print $2}'
Tip#02
   
    To find the count of lines in a file.
   
    wc file-name | awk '{ print $1 }'
Tip#03

    To get the particular lines which contains the specific pattern
   
    cat file-name | awk '/pattern/ { print }'
Tip#04

    To print only the specific fields.
    Example to get the modification date time of a file.
   
    ll <Directory-Location>| awk '/file-name/ { print $6,$7,$8 }'
Tip#05

    To calculate the column values
   
    input_file

    hai 5 times
    bye 2 times
    equalto 7 times talking
   
    awk "{ total += \$2 } END { print total    }" "input_file"
Tip#06

    To find the file size and printing with strings for the identification
   
    ls -l <file-name> | awk '{ print $9 "    File size: " $5 }'
Tip#07

    Pattern separation with delimiter
   
    Input
        root:kQnW9dBGlBs6Q:0:3:exit,pwd,33333,22222:/:/usr/bin/ksh
   
    To get the passwd filed of two,
   
    cat input | awk 'BEGIN {FS=":"} { print $2 }'
Regular expressions with SED and AWK
Muthukumar Kandasamy
[email protected]
 

 


 
Hosted by www.Geocities.ws

1