Professional Documents
Culture Documents
brian@thecodecave.com
Home
Gone Caving?
Did that help?
When working toward updating my upgrade script to read the usernames and passowords from the wp-config files, I
came across this helpful list:
————————————————————————-
USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor) Dec. 29, 2005
Compiled by Eric Pement – pemente[at]northpark[dot]edu version 5.5
FILE SPACING:
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
sed ‘/^$/d;G’
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 2 of 13
# insert a blank line above and below every line which matches “regex”
sed ‘/regex/{x;p;x;G;}’
NUMBERING:
# number each line of a file (simple left alignment). Using a tab (see
# note on ‘\t’ at end of file) instead of space will preserve margins.
sed = filename | sed ‘N;s/\n/\t/’
# number each line of file, but only print numbers if line is not blank
sed ‘/./=’ filename | sed ‘/./N; s/\n/ /’
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 3 of 13
# substitute “foo” with “bar” ONLY for lines which contain “baz”
sed ‘/baz/s/foo/bar/g’
# substitute “foo” with “bar” EXCEPT for lines which contain “baz”
sed ‘/baz/!s/foo/bar/g’
# add commas to numbers with decimal points and minus signs (GNU sed)
gsed -r ‘:a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta’
# 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
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 4 of 13
# print only lines which do NOT match regexp (emulates “grep -v”)
sed -n ‘/regexp/!p’ # method 1, corresponds to above
sed ‘/regexp/d’ # method 2, simpler syntax
# print the line immediately before a regexp, but not the line
# containing the regexp
sed -n ‘/regexp/{g;1!p;};h’
# print the line immediately after a regexp, but not the line
# containing the regexp
sed -n ‘/regexp/{n;p;}’
# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to “grep -A1 -B1″)
sed -n -e ‘/regexp/{=;x;1!p;g;$!N;p;D;}’ -e h
# 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’
# print paragraph if it contains AAA and BBB and CCC (in any order)
sed -e ‘/./{H;$!d;}’ -e ‘x;/AAA/!d;/BBB/!d;/CCC/!d’
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 5 of 13
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 6 of 13
# delete all CONSECUTIVE blank lines from file except the first; also
# deletes all blank lines from top and end of file (emulates “cat -s”)
sed ‘/./,/^$/!d’ # method 1, allows 0 blanks at top, 1 at EOF
sed ‘/^$/N;/\n$/D’ # method 2, allows 1 blank at top, 0 at EOF
# delete all CONSECUTIVE blank lines from file except the first 2:
sed ‘/^$/N;/\n$/N;//D’
SPECIAL APPLICATIONS:
# remove nroff overstrikes (char, backspace) from man pages. The ‘echo’
# command may need an -e switch if you use Unix System V or bash shell.
sed “s/.`echo \\\b`//g” # double quotes required for Unix environment
sed ’s/.^H//g’ # in bash/tcsh, press Ctrl-V and then Ctrl-H
sed ’s/.\x08//g’ # hex expression for sed 1.5, GNU sed, ssed
# parse out the address proper. Pulls out the e-mail address by itself
# from the 1-line return address header (see preceding script)
sed ’s/ *(.*)//; s/>.*//; s/.*[:<] *//’
# add a leading angle bracket and space to each line (quote a message)
sed ’s/^/> /’
# delete leading angle bracket & space from each line (unquote a message)
sed ’s/^> //’
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 7 of 13
# zip up each .TXT file individually, deleting the source file and
# setting the name of each .ZIP file to the basename of the .TXT file
# (under DOS: the “dir /b” switch returns bare filenames in all caps).
echo @echo off >zipup.bat
dir /b *.txt | sed “s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/” >>zipup.bat
TYPICAL USE: Sed takes one or more editing commands and applies all of
them, in sequence, to each line of input. After all the commands have
been applied to the first input line, that line is output and a second
input line is taken for processing, and the cycle repeats. The
preceding examples assume that input comes from the standard input
device (i.e, the console, normally this will be piped input). One or
more filenames can be appended to the command line if the input does
not come from stdin. Output is sent to stdout (the screen). Thus:
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 8 of 13
it is heartening to know that GNU sed will let you reduce it to:
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 9 of 13
Comments
Can you share what you’ve come up with so far on the DB backup script?
DONE -brian
I cannot figure out how to split a line on a PIPE delimited file on a windows server. I have tried googling for hours
and nothing works. The file has a space between each line. If I use \n all that happens is the pipe is replaced with
the letter n.
CALL|TITLE|URL LINK|
CALL|TITLE|URL LINK|
Name (required)
Website
Submit Comment
Subscribe
Subscribe to RSS
Comments
Recent Posts
Brian Layman’s Weekly Twitter Update for 2010-03-07
Brian Layman’s Weekly Twitter Update for 2010-02-28
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 10 of 13
Archives
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 11 of 13
January 2009
December 2008
November 2008
August 2008
July 2008
June 2008
May 2008
March 2008
February 2008
January 2008
November 2007
October 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
Links
(Mad)Crab Volleyball - A game I hacked for a friend of mine
404 by Count Null - A German site with decent information about WordPress and Themes
Aaron Brazell – Technosailor - Technology, blogging, Web 2.0 and Entrepreneurship
Alex King - A prominent WordPress Developer
Andy Skelton (skeltoac) - Automattic Code wrangler
Balloon Headed Boy - A fun kids game
Christian Music Fan - A site for conversations on Christan music.
Cindy’s Digital Ramble - Winner of the First-Blog-I-Wandered-Into-That-Has-Me-In-Their-Blogroll
unannounced contest.
Computer Guru - An AVID WP-Hacker
- ConTEXT is a very powerful editor with syntax highlighting for most kinds of source
code. It’s written in Delphi, of course! Finally, a worthy Notepad replacement,
Craig Hartel - The Nuclear Moose is no longer a Hacker but still has proment sites…
David Chait - WP-Hacker and enthusiast
Donncha O’Caoimh - Lead Developer of WordPress.com
Doug Stewart - A very bright WP-Hacker with a FUN site to read
Dougal’s Geek Ramblings - Dougal’s, a WP-Hacker, Geek Ramblings
Handy Solo - Support Forum Guru
Hapland 1 - Can you click and solve this page? I did..
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 12 of 13
Hapland 2 - Ah, but can you open this portal? It’s harder!
Hapland 3 - The final chapter
Iggy the Biker - Because you’re never too old to ride…
Installing WordPress on Windows - A description of how to install WordPress locally
io_error - IRC Chat, Liberty, WordPress and the American way…
Justin - Justin is a WP-Hacker
Knitting Loom Central - A website for free knitting patterns
- This IE shell utility turns IE into a browser that tops all competition in many ways
including allowing Delphi built extensions.
Michael / miklb - A VERY active WP Support forum guy
Michael Adams-mdawaffe - CalTech based Automattic Dude
Michel Valdrighi - Developer of WP’s predicessor: B2
Mike Little - Co-Founded WordPress with Matt Mullenweg.
My Infocom Games - This is how I play my infocom games while on the road
Owen Winkler - A down to earth WP-Hacker and all around good guy.
Peter Westwood (Westi) - WP Hacker that helps keep WordPress on Trac
Planet Sean - Technology Goodness at its best!!!!
Podz - A VERY active WP Support forum guy
Purls Of Hope - A charity site for Hurricane Katrina victims
Rhett And LinKast Live - I help out with the Rhett and LinKast Live every week. Good Clean fun!
Thursdays at 9pm
Rob Miller - Rob Miller’s blog of helpful hints and plug ins…
Robert Deaton - A WP-Hacker with strong coding skills and even stronger opinions ;)
Roy Schevstowitz - WPHacker – Smart guy, Runs a Heavily hacked WP 1.2!!! site safely.
Ryan Boren - Ryan is one of the Automattics. Matt’s #1 I should think.
spencerp - Good guy with a lot of WP-Hacker suggestions
Submitting Bug Reports & Feature Requests - Describes how to tell the WP developers about a bug or
improvement
Submitting Patches with Your Code Changes - The best way to ensure your request is approved, is to submit
the code changes.
The 298 Sand Bandits - A page for the wives of my bro-inlaw’s unit
The Bored Again Christian - Bored with Christian music? Here’s a podcast with some unique sounds…
The WildWest Forums - A VBulletin Forum for the WildWest mod
The WildWest RtCW Mod - Support for The WildWest mod
Viper007Bond - A WP-Hacker – I admire his ingenuity and little utilities
William Lehman’s Decloned - Cloneliness is NOT next go Godliness!
- Kim Henkel’s ZTree program allows you to do so many file management tasks simply
not possible in Explorer that Bill Gates decided it was way to helpful to include in WindowsXP.
Meta
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010
A thorough list of SED command line examples : The Code Cave Page 13 of 13
Log in
WordPress
XHTML
Cave Drips...
while ($coffee < MIN_COFFEE) $coffee++; 18 hrs ago
I subscribed to JohnsHouseofFun's channel on YouTube http://www.youtube.com/user/JohnsHouseofFun?
feature=autoshare_twitter 2 days ago
More updates...
Graffiti
404 error files html Internet explorer IIS b5media ball girl ballgirl bed time stories beta Blog World and New Media Expo Bookmarklets brianlayman
code ninja
children books eHermit Contest Donncha Facebook geocaching geo location gps Lorelle Mafia Wars Mafia Wars: Cuba merge
micro blogging
Michael Widenius Monty MySQL PHP PHP 4 PHP 5 phpMyAdmin Screenshots sham wow Sun Microsystems sxsw trade
twitter WordCamp SF WordPress WordPress 2.6 WordPress 2.7 WordPress mu WordPress PHP MySQL wp-hackers YouTube Zynga
Recently Stored
Brian Layman’s Weekly Twitter Update for 2010-03-07
Brian Layman’s Weekly Twitter Update for 2010-02-28
Brian Layman’s Weekly Twitter Update for 2010-02-21
How to: Find files edited in the last day
Brian Layman’s Weekly Twitter Update for 2010-02-14
Brian Layman’s Weekly Twitter Update for 2010-02-07
Brian Layman’s Weekly Twitter Update for 2010-01-31
http://www.thecodecave.com/a-thorough-list-of-sed-command-line-examples/ 3/12/2010