sed - stream editor.

sed "edit-command" file[s]-to-be-edited > edited-file
  • Non-interactive or batch mode editor.
  • Edits one line at a time.
  • Multiple edit commands applied to one line at a time, if appropriate.
  • Original file remains intact unless used with -i option.
  • If multiple data files specified, treated as a single data input stream.
  • Regular expressions are available and extensively used in sed commands.
  • All activity is case sensitive.
  • If multiple edit statements are applied, they are applied in order listed.
    If you have a file in which you want to change all occurrences of dogs to 
    hounds and all occurrences of cats to dogs. 
    
    If you change the cats to dogs 1st and then dogs to hounds, the cats
    will also become hounds. So do the dogs to hounds 1st.
    

    Various Options
  • -n no output unless explicitly commanded. Can turn sed into a grep with editing ability.
  • -e edit-sequence define an edit rule on the command line. Can be used multiple times.
  • -f edit-cmd-file use named file for source of edit commands. Avoids mis-interpretation of wild-cards and $ reference.
  • -i edit the original target file. (GNU version?)
  • -r use extended regular expressions. (GNU version)
  • -s if multiple data files named, each treated separately. (GNU version)
    Commands
  • s - substitute Probably the most common and useful command. s/match/replacement/ All of the standard regular expressions are available for the match. The replacement is a literal string in most cases and can consist of most characters, a few may have to be escape quoted, \. Each line of data file is examined for an occurrence of match. If found, it is replaced with replacement. s/match-string/replacement/ - only the 1st occurrence of the match is replaced on any particular line. s/match-string/replacement/g - global, will substitute all occurrences on each line. s/match-string/replacement/# where # is a numeric value. s/match-string/replacement/2 targets only the 2nd occurrence on the line. Can be combined with looping commands to edit remaining occurrences. While / is the standard delimiter with the substitute command, You may immediately follow the s with almost any character and that character becomes the delimiter. * Otherwise you would have to escape quote, \, any literal forward slashes. s#/User#/usr/local#g
    Exceptions to literal nature of the replacement. You may use the & on the replacement side to recall the full match. ls | sed "s/^.*$/mv & &.bak/" > mvlist.sh - will list all files in current directory. The sed will then output a line, replacing the original line with with the command mv followed by a copy of the whole line which should be a filename followed by another copy of the whole line with a .bak appended. The output is written to the mvlist.sh sed -i "s#\<Northern Illinois University\>#<b>&</b>#g page1.html This puts edited lines back in original file. It adds a bold markup to any occurrence of the full Northern name. In this situation, using word delimiters probably overkill. The & may be used with the -r (extended regex) option.
    The \(\) can be used to remember portions of the string matching the match expression, You can recall that part of the string in both the match side and the replacement side of the substitute. sed "s/\<\([[:alpha:]][[:alpha:]]*\) \1\>/\1/g" dfile1 > dfile.fixed Will look for any repeated words of one or more alpha characters separated by a single space, If found, replaces them with just a single instance of the word. Multiple occurrences may be used and are numbered from the left. s/^\([[:alpha:]][[:alpha:]]*\) \([[:alpha:]][[:alpha:]]*\)\>/\2 \1/ will swap the 1st and 2nd word on each line. The \( \) and \# cannot be used with the -r (extended regex) option.
  • Unless the -n option is used, all lines read in will be printed out with or without modification. p modifier. The p can be appended to the substitute command to force printing of a line which is successfully modified. sed -n "s/President/Mr./2p" letter This should print out only lines in which the second occurrence of the string President is replaced with Mr. The -n option and p command can be used independent of each other but are most commonly used together.