sed

From Initq

Jump to: navigation, search

sed (Stream EDitor) refers to a Unix utility for parsing text files and the programming language it uses to apply textual transformations to a sequential stream of data. It reads input files line by line, applying the operation which has been specified via the command line (or a sed script), and then outputs the line. It was developed from 1973 to 1974 as a Unix utility by Lee E. McMahon of Bell Labs[1], and is available today for most operating systems.The sed command directly modifies the content of files, sending the changed file to stdout.

 sed [options] -f script-file [input-file]
 sed [options] script-text [input-file]
Command Addresses Meaning
= 0 or 1 Display the current line number
a\text 0 or 1 Append text to the file
i\text 0 or 1 Insert text into the file
r filename or 1 Append text from filename into the file
c\text range replace the selected range
s/regexp/replacement range replace text that matches the regular expression (reqexp) with replacement
root@debian60:~/junk# cat computer.txt 
ibm 9000
root@debian60:~/junk# sed -e 'i\text' computer.txt 
text
ibm 9000
root@debian60:~/junk# sed -e 'a\text' computer.txt 
ibm 9000
text
 
root@debian60:~/junk# sed '/^if/a #hello there' profile 
# ~/.profile: executed by Bourne-compatible login shells.
 
if [ "$BASH" ]; then
#hello there
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi
 
mesg n
root@debian60:~/junk# sed '/^if/i #hello there' profile 
# ~/.profile: executed by Bourne-compatible login shells.
 
#hello there
if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi
 
mesg n
Personal tools