Check out my first novel, midnight's simulacra!

Sed: Difference between revisions

From dankwiki
(Created page with "The '''s'''tream '''ed'''itor. Sometimes the '''s'''hitty '''ed'''itor. <tt>sed</tt> works on lines at a time (mostly). Note that GNU and POSIX <tt>sed</tt> differ in some sig...")
 
 
Line 5: Line 5:
==Recipes==
==Recipes==
===Join pairs of lines===
===Join pairs of lines===
<tt>sed 'N;s/\n/,/'</tt>
<tt>sed 'N;s/\n/ /'</tt> replaces every other newline with a space. Replace the space with whatever you want.

Latest revision as of 13:14, 23 August 2020

The stream editor. Sometimes the shitty editor. sed works on lines at a time (mostly). Note that GNU and POSIX sed differ in some significant ways.

Use -i to run sed on a file inline. Never use e.g. cat file | sed > file; the shell redirection is going to open file for writing with truncation, which is probably not what you want. Supply expressions on the command line with -e, and/or files of expressions with -f.

Recipes

Join pairs of lines

sed 'N;s/\n/ /' replaces every other newline with a space. Replace the space with whatever you want.