SED Command

Sed is the ultimate stream editor. SED is the most powerful UNIX command. In its simplest form,
[oracle@usha scripts]$ echo day |sed 's/day/night/' will return
night
# convert DOS newline to Unix format

sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M

# Print lines from a file not starting with -- and also remove blank lines

cat dbaref.lst|sed -n '/\-/!p'|sed '/^$/d'
 
"/^$"  represents a line that begins and ends without text.

# add text at the begining and end of the line.

[oracle@usha scripts]$ cat allsql
a.sql
test.sql
[oracle@usha scripts]$

[oracle@usha scripts]$ cat allsql|sed 's/^/@/'
@a.sql
@test.sql

[oracle@usha scripts]$ cat allsql|sed 's/^/@/'|sed 's/[\t]*$/;/'
@a.sql;
@test.sql;

# delete leading whitespace (spaces, tabs) from front of each line

sed 's/^[ \t]*//'

[oracle@usha scripts]$ cat a                       # Original file
  Sachchida Nand Ojha   
  Andrew Bove
 
[oracle@usha scripts]$ cat a|sed 's/^[ \t]*//'    #deleted all leading space
Sachchida Nand Ojha   
Andrew Bove

[oracle@usha scripts]$ cat a|sed 's/^[ \t]//'       # Deleted one space
 Sachchida Nand Ojha   
 Andrew Bove

[oracle@usha scripts]$ cat a|sed 's/^[\t]*//'        # Does Nothing
  Sachchida Nand Ojha   
  Andrew Bove
 
[oracle@usha scripts]$
                

# delete trailing white space (spaces, tabs) from end of each line

sed 's/[ \t]*$//'    

[oracle@usha scripts]$ cat a|sed 's/[ \t]*$//'
  Sachchida Nand Ojha
  Andrew Bove            

# delete BOTH leading and trailing white spaces from each line

sed 's/^[ \t]*//;s/[ \t]*$//'


[oracle@usha scripts]$ cat a|sed 's/^[ \t]*//';sed 's/[ \t]*$//'
Sachchida Nand Ojha   
Andrew Bove

#changing PATH from /usr/local/bin to /common/bin

[oracle@usha scripts]$ cat a
  Sachchida Nand Ojha   
  Andrew Bove

PATH=/usr/local/bin
 
[oracle@usha scripts]$ cat a|sed 's/\/usr\/local\/bin/\/common\/bin/'
  Sachchida Nand Ojha   
  Andrew Bove

PATH=/common/bin

[oracle@usha scripts]$  cat a|sed 's:/usr/local/bin:/common/bin:'
  Sachchida Nand Ojha   
  Andrew Bove

PATH=/common/bin
 
[oracle@usha scripts]$ cat a|sed 's|/usr/local/bin|/common/bin|'
  Sachchida Nand Ojha   
  Andrew Bove

PATH=/common/bin
 
[oracle@usha scripts]$





Comments