Prashant Mhatre

Make Money, Programming, Bollywood, Marathi

  • You are here: 
  • Home
  • Unix Stream Editor Sed Tips Tricks

Unix Stream Editor Sed Tips Tricks

Posted on February 18th, 2008


-- Sponsored Links --


This is a short version of the Sed tips published at http://sed.sourceforge.net/sed1line.txt
Computer

Adjust File Spacing


-- Sponsored Links --


# double space a file
sed G

# triple space a file
sed ‘G;G’

# insert a blank line above every line which matches “REGULAR_EXPRESSION”
sed ‘/REGULAR_EXPRESSION/{x;p;x;}’

# insert a blank line below every line which matches “REGULAR_EXPRESSION”
sed ‘/REGULAR_EXPRESSION/G’

# insert a blank line above and below every line which matches “REGULAR_EXPRESSION”
sed ‘/REGULAR_EXPRESSION/{x;p;x;G;}’

Text Conversion and Substitution

# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
sed ‘s/^[ \t]*//’ # see note on ‘\t’ at end of file

# delete trailing whitespace (spaces, tabs) from end of each line
sed ‘s/[ \t]*$//’ # see note on ‘\t’ at end of file

# delete BOTH leading and trailing whitespace from each line
sed ‘s/^[ \t]*//;s/[ \t]*$//’

# insert 5 blank spaces at beginning of each line (make page offset)
sed ‘s/^/ /’

# align all text flush right on a 79-column width
sed -e :a -e ‘s/^.\{1,78\}$/ &/;ta’ # set at 78 plus 1 space

# substitute (find and replace) “FIND_TERM” with “REPLACE_TERM” on each line
sed ‘s/FIND_TERM/REPLACE_TERM/’ # replaces only 1st instance in a line
sed ‘s/FIND_TERM/REPLACE_TERM/4′ # replaces only 4th instance in a line
sed ‘s/FIND_TERM/REPLACE_TERM/g’ # replaces ALL instances in a line
sed ‘s/\(.*\)FIND_TERM\(.*FIND_TERM\)/\1REPLACE_TERM\2/’ # replace the next-to-last case
sed ‘s/\(.*\)FIND_TERM/\1REPLACE_TERM/’ # replace only the last case

# substitute “FIND_TERM” with “REPLACE_TERM” ONLY for lines which contain “baz”
sed ‘/baz/s/FIND_TERM/REPLACE_TERM/g’

# substitute “FIND_TERM” with “REPLACE_TERM” EXCEPT for lines which contain “baz”
sed ‘/baz/!s/FIND_TERM/REPLACE_TERM/g’

# change “scarlet” or “ruby” or “puce” to “red”
sed ‘s/scarlet/red/g;s/ruby/red/g;s/puce/red/g’ # most seds
gsed ‘s/scarlet\|ruby\|puce/red/g’ # GNU sed only

# join pairs of lines side-by-side (like “paste”)
sed ‘$!N;s/\n/ /’

# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
gsed ’0~5G’ # GNU sed only
sed ‘n;n;n;n;G;’ # other seds

Selective Printing of Certain Lines

# print first 10 lines of file (emulates behavior of “head”)
sed 10q

# print first line of file (emulates “head -1″)
sed q

# print the last 10 lines of a file (emulates “tail”)
sed -e :a -e ‘$q;N;11,$D;ba’

# print the last 2 lines of a file (emulates “tail -2″)
sed ‘$!N;$!D’

# print the last line of a file (emulates “tail -1″)
sed ‘$!d’ # method 1
sed -n ‘$p’ # method 2

# print only lines which match regular expression (emulates “grep”)
sed -n ‘/REGULAR_EXPRESSIONp/p’ # method 1
sed ‘/REGULAR_EXPRESSIONp/!d’ # method 2

# print only lines which do NOT match REGULAR_EXPRESSIONp (emulates “grep -v”)
sed -n ‘/REGULAR_EXPRESSIONp/!p’ # method 1, corresponds to above
sed ‘/REGULAR_EXPRESSIONp/d’ # method 2, simpler syntax

# print the line immediately before a REGULAR_EXPRESSIONp, but not the line
# containing the REGULAR_EXPRESSIONp
sed -n ‘/REGULAR_EXPRESSIONp/{g;1!p;};h’

# print the line immediately after a REGULAR_EXPRESSIONp, but not the line
# containing the REGULAR_EXPRESSIONp
sed -n ‘/REGULAR_EXPRESSIONp/{n;p;}’

# grep for AAA and BBB and CCC (in any order)
sed ‘/AAA/!d; /BBB/!d; /CCC/!d’

# grep for AAA and BBB and CCC (in that order)
sed ‘/AAA.*BBB.*CCC/!d’

# grep for AAA or BBB or CCC (emulates “egrep”)
sed -e ‘/AAA/b’ -e ‘/BBB/b’ -e ‘/CCC/b’ -e d # most seds
gsed ‘/AAA\|BBB\|CCC/!d’ # GNU sed only

# print only lines of 65 characters or longer
sed -n ‘/^.\{65\}/p’

# print only lines of less than 65 characters
sed -n ‘/^.\{65\}/!p’ # method 1, corresponds to above
sed ‘/^.\{65\}/d’ # method 2, simpler syntax

# print section of file from regular expression to end of file
sed -n ‘/REGULAR_EXPRESSIONp/,$p’

# print section of file based on line numbers (lines 8-12, inclusive)
sed -n ’8,12p’ # method 1
sed ’8,12!d’ # method 2

# print line number 52
sed -n ’52p’ # method 1
sed ’52!d’ # method 2
sed ’52q;d’ # method 3, efficient on large files

# print section of file between two regular expressions (inclusive)
sed -n ‘/Prashant/,/Mhatre/p’ # case sensitive

Selective Deletion Of Certain Lines

# print all of file EXCEPT section between 2 regular expressions
sed ‘/Prashant/,/Mhatre/d’

# delete the first 10 lines of a file
sed ’1,10d’

# delete the last line of a file
sed ‘$d’

# delete the last 2 lines of a file
sed ‘N;$!P;$!D;$d’

# delete the last 10 lines of a file
sed -e :a -e ‘$d;N;2,10ba’ -e ‘P;D’ # method 1
sed -n -e :a -e ’1,10!{P;N;D;};N;ba’ # method 2

# delete every 8th line
gsed ’0~8d’ # GNU sed only
sed ‘n;n;n;n;n;n;n;d;’ # other seds

# delete lines matching pattern
sed ‘/pattern/d’

# delete ALL blank lines from a file (same as “grep ‘.’ “)
sed ‘/^$/d’ # method 1
sed ‘/./!d’ # method 2

# delete all leading blank lines at top of file
sed ‘/./,$!d’

Filed under Programming |

Comments are closed.