sed cheat

Home

1 sed the stream editor

The original 'ed' was used in UofT CSC classes in 1982 (and probably earlier). sed is a stream editor that simply makes changes to the contents of a stream.

Commonly used to replace strings with other strings (or null if you want to delete the strings) on some stream of data. i.e. a pipe.

By default the output is stdout and by default the input is a pipe, or a file if you select that file on the command line. i.e.:

  • sed 's/.*junk on line//' myfile.txt # removes all characters up to and

Compare that to:

  • cat myfile.txt | sed 's/.*junk on line//' # removes all characters up to

2 Misc sed cheats.

  • sed 's/ Ont./ Ontario/; s/ ON/ Ontario/' mailing-list

or equivalently:

  • sed -e 's/ Ont./ Ontario/' -e 's/ ON/ Ontario/' mailing-list
  • sed 's/ zperkons/ Zintis Perkons/' reports*

(for all files beginning with "reports" )

  • sed -f scriptfile mailing-list

(where scriptfile contains the following : ) s/ Ont./ Ontario/ s/ ON/ Ontario/ s/ Dr./ Drive/ s/ St./ Street/

  • sed '/^$/d' input-file (deletes all the blank lines of input-file)
  • sed '/^#/d' input-file (deletes all lines beginning with #, i.e. comments)
  • sed '/^$/d ; /^#/d' input-file ( does BOTH the above, i.e. strip blank lines, and strip comments )
  • sed '1,/^$/d' input-file (deletes from the first line, up to the first occurance of a blank line)
  • sed '1,/Jul 31/d' cisco.log (deletes all lines in the cisco.log before Jul 31)
  • sed '1,/Jul 31/s/nmeventd/NM event daemon/g' cisco.log (I don't know why someone would want to do this particalar example but it will change all occurances of nmeventd to "NM event daemon" for lines up to Jul 31 in cisco.log, leaving all lines after that date UNCHANGED! –> i.e. the default action for lines that do not match the addressing criteria is to print (echo) to output! )
  • sed '1,/Jul 31/{/^$/d}' input-file This will nest the action in the braces to act on the lines matched in the first address criteria : i.e. it will delete all blank lines occuring before Jul 31!
foreach filename (*.sh)
sed 's/ON/Ontario/; s/St/Street/; s/Rd/Road/' $filename > $filename.new
end 

sed 's/^/"/; s/,/","/ ; s/$/," "," "," "," "," "," "," "," "/' xray-hosts > hosts.csv

# 
#  to remove all the extra spaces that yaz complained about in my python code
#  I created this sed script.  Basically it changes stuff like "print ( var1" to 
#  "print(var"   and "list1 [ 'apple'," to "list1['apple,"   
#  and speaking of lists, ['string1', 'string2'] and not [ 'string1','string2' ]
#  BTW, these all work, and are only a question of python style.
#

foreach filename (*.py)
sed -f fix-py-style-sed-script $filename > $filename.ppyy
end

where fix-py-style-sed-script is a script containing these lines:

s/ ) /)/g
s/ )/)/g
s/) /)/g
s/ : /:/g
s/ :/:/g
s/: /:/g
s/( /(/g
s/ ( /(/g
s/ (/(/g
s/\[ /\[/g
s/ \] /\]/g
s/ \]/\]/g
s/\] /\]/g
s/ ,/,/g
s/,/, /g

3 Scripting using fish shell

3.0.1 Edit multiple files at once using sed

for file in *.org
   sed 's/zintis AT gmail DOT com/zintis AT senecacollege DOT ca/' $file > $file.new
end

check the results and if good:

for file in *.org.new
   mv "$file" "{$file%.new}.old"
end

3.0.2 Multiple file extension change.

for x in *.html; do mv "$x" "${x%.html}.php"; done

This script will change all files in current directory ending with html extension to

php extension. (*.html to *.php), You can customize it as per your requirment.

3.1 Home