Friday, July 26, 2013

STREAM EDITOR - SED

SED is a multipurpose tool which combines the work of several filters.
Ex:       sed options ‘address action’ file(s)

Addressing in sed is done in two ways:
          By one or two line nos.
          By specifying /pattern/
Line Addressing:
$ sed ‘3q’ emp.lst
Displays first 3 lines of the file and quits from sed.
$ sed –n ‘1,3p’ emp.lst
Displays first 3 lines of the file. (p and n must be used)
$ sed –n ‘$p’ emp.lst
Displays the last line of the file
$ sed –n ‘1,2p
7,9p’ emp.lst
Displays selective groups of lines
$ sed –n ‘3,$!p’ emp.lst
Do not print the lines from 3 to the end of the file.
Using Multiple Instructions:
$ sed –n –e ‘1,2p’ –e ‘7,9p’ emp.lst
Putting instructions in a file:
$ cat > patfile
1,2p
7,9p
^d
$ sed –n –f patfile emp.lst
Context Addressing:
$ sed –n ‘/director/p’ emp.lst
Displays all the lines that contain ‘director’
$ sed –n ‘/director/, /manager/p’ emp.lst
Displays all the lines from director to manager
$ sed –n ‘1, /director/p’ emp.lst
Line nos. and context addresses can be mixed
$ sed –n ‘/^a/p’ emp.lst 
Displays all the lines that start with ‘p’ (regular exp.)
Writing selected lines to a file:
$ sed –n ‘/director/w dlist’ emp.lst
$ sed –n ‘/director/w dlist
/manager/w mlist’ emp.lst
Text Editing:
Inserting            i
Appending        a
Changing          c
Deleting            d
$ sed ‘1i\
abc \
pqr’ emp.lst
$ sed ‘1a\
abc\
pqr’ emp.lst
$ sed ‘1c\
abc’ emp.lst
$ sed ‘/director/d’ emp.lst
SUBSTITUTION:
$ sed ‘s/director/director1/g’ emp.lst
$ sed ‘1,5 s/director/director1/g’ emp.lst
MULTIPLE SUBSITUTIONS:
$ sed ‘s/i/m/g
s/x/y/g’ emp.lst


$ find path –name pattern –print checks for pattern in the path and displays the file
list
$ zip xyz.zip abc pqr                             zips abc, pqr into xyz.zip
$ unzip –v xyz.zip                                  unzips a zipped file
$gzip xyz.lib                                          creates a zipped file xyz.lib.gz
$gunzip xyz.lib.gz                                  retrieves all the files
$dos2unix abc abc.unix             converts a file from dosformat to unixformat
$unix2dos abc.unix abc             converts a file from unix format to dosformat
$ at <time> <command list>                  tells unix to execute a set of commands at the given time
$ batch <commad list>              unix tells when to execute the set of commands
$ ps                                                      lists the processes along with id running on the current
                                                            terminal
$ stty                                                    sets and tells the terminal settings
$ fg                                                      brings the processes foreground
$ bg                                                     brings the processes background
$ time                                                   tells different times taken for a command to run




No comments:

Post a Comment