[Overview]sed Examples
sed - stream editor for filtering and transforming text
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n: supress automatic printing of pattern space
-e: <script>: add script to the commands to be executed
-f: <script-file>: run commands in <script-file>
-i: edit file inplace
-E: use extended regexs
for further information see the manual page sed(1)
- replaces all occuences of the isolated character D with GER and prints the result to STDOUT
sed 's/\bD\b/GER/' ../data/city.sample.csv
[Output]
- replaces in each line the first occurence of NULL with \N
sed 's/NULL/\\N/' ../data/city.sample.csv
[Output]
- replaces all occurences of NULL in the first 200 lines with \N (g: global)
sed '1,200 s/NULL/\\N/g' ../data/city.sample.csv
[Output]
- delete all lines, containing the string USA (output to STDOUT)
sed '/USA/ d' ../data/city.sample.csv
[Output]
- delete all lines, containing the string USA and writes the result to the original file (inplace editing)
cp ../data/city.sample.csv ../data/city.txt && sed -i '/USA/ d' ../data/city.txt
[Output]
- delete all lines ending with NULL,NULL
sed '/NULL,NULL$/ d' ../data/city.sample.csv
[Output]
- adds the entry for Bruchsal at line 1
sed '1 i Bruchsal,D,"Baden Wuerttemberg",72753,50,6' ../data/city.sample.csv
[Output]
- adds (appends) the entry for Leopoldshafen after line 1 (inplace)
cp ../data/city.sample.csv ../data/city.txt && sed -i '1 a Leopoldshafen,D,"Baden Wuerttemberg",NULL,NULL,NULL' ../data/city.txt
[Output]
- change the entry at address /Leopoldshafen,D/ to the given string
sed '/Leopoldshafen,D/ c Leopoldshafen,GER,"Baden Wuerttemberg",52873,49,6' ../data/city.txt
[Output]
- replaces the first 100 lines with a single comment line
sed '1,100 c // the first 100 lines have been deleted' ../data/city.sample.csv
[Output]
- adds the content of ../data/city-D.csv after each line matching the string Hildesheim,D (should only be one in this case ;-))
sed '/Hildesheim,D/ r ../data/city-D.csv' ../data/city.sample.csv
[Output]
- deletes the first 100 lines
sed '1,100d' ../data/city.sample.csv
[Output]
- print lines, starting from line 3000 (-n: supress default printing behaviour)
sed -n '3000,$ p' ../data/city.sample.csv
[Output]
- remove Guttenberg specific boilerplate text
sed '1,/^\*\*\* START OF THIS PROJECT/d;/^\*\*\* END OF THIS PROJECT/,$d ' ../data/moby-dick.txt
[Output]
- underline each line, starting with the string CHAPTER
sed '/^CHAPTER / a--------------------' ../data/moby-dick.txt
[Output]
- remove the guttenberg specific header and trailer parts from the book
sed '1,/^\*\*\* START OF THIS PROJECT GUTENBERG EBOOK MOBY/ d' ../data/moby-dick.txt | sed '/^\*\*\* END OF THIS PROJECT GUTENBERG EBOOK/,$ d'
[Output]
- simple method to test the behaviour of sed
seq 100 120 | sed '4,7 i X'
[Output]
- delete all empty lines
sed '/^ *$/d' ../data/moby-dick.txt
[Output]
- remove all <script> ... </script> sections and all tags (in a first step, we move the all script tags in separate lines)
sed -E 's/(<\/?script[^>]*>)/\n\1\n/' ../data/new_york_wiki.html | sed -E '/<script[^>]*>/,/<\/?script>/d' | sed -E 's!</?[^>]*>!!g'
[Output]
all examples assembled by andreas schmidt for the DBKDA 2021 conference