Sed: Difference between revisions

From Psygen Wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by 2 users not shown)
Line 35: Line 35:


<code>r <i>filename</i></code> Read File
<code>r <i>filename</i></code> Read File
'''Substitution:''' <br />
<code>s/..../..../</code> Substitute. Changes the 1st pattern to the second pattern.<br />
'''Example:''' <code>sed 's/red/blue/' < file.txt</code> Changes "red" to "blue" in file.txt <br />
&nbsp; &nbsp; <code>cat file.txt | sed 's/one/two/'</code> Changes "one" to "two" in file.txt
The search pattern is on the left, and the replacement string is on the right. By default, sed replaces the first occurrence per line.


<code>t label</code> Test
<code>t label</code> Test
Line 52: Line 42:
<code>x eXchange
<code>x eXchange


<code>y/..../..../</code> Transform
<code>y/..../..../</code> - Transform
 
<code>s/..../..../</code> - Substitute
 


== sed Pattern Flags ==
== sed Pattern Flags ==
Line 58: Line 51:
<code>/g</code> Global. By default, sed only matches/replaces the first occurrence of a pattern for each line. The /g makes it replace all occurrences.<br />
<code>/g</code> Global. By default, sed only matches/replaces the first occurrence of a pattern for each line. The /g makes it replace all occurrences.<br />
'''Example:''' <br />
'''Example:''' <br />
<code>sed 's/red/blue/g' < file.txt</code>
<code>sed 's/red/blue/g' < file.txt</code><br />
 


<code>/I</code> Ignore Case
<code>/I</code> Ignore Case
Line 65: Line 59:


<code>/w <i>filename</i></code> Write Filename
<code>/w <i>filename</i></code> Write Filename


== Special Characters ==
== Special Characters ==
Line 78: Line 71:


<code>sed 's/[a-z]*/(&)/' <old.txt >new.txt </code>
<code>sed 's/[a-z]*/(&)/' <old.txt >new.txt </code>




Line 85: Line 77:
<code> -r </code> &nbsp; Enable sed to use [[Regular Expressions]]
<code> -r </code> &nbsp; Enable sed to use [[Regular Expressions]]


<code> -e </code> The -e option lets you combine multiple commands
<code>sed -e 's/a/A/' -e 's/b/B/' <old.txt >new.txt</code>
<code> -i </code> Inline editing.
== Substitution ==
<code>s/..../..../</code> Substitute. Changes the 1st pattern to the second pattern.<br />
'''Example:'''<br />
<code>sed 's/red/blue/' < file.txt</code> Changes "red" to "blue" in file.txt <br />
or:<br />
<code>sed -i 's/red/blue/' file.txt</code> Changes "red" to "blue" in file.txt <br />
&nbsp; &nbsp; <code>cat file.txt | sed 's/one/two/'</code> Changes "one" to "two" in file.txt
The search pattern is on the left, and the replacement string is on the right. By default, sed replaces the first occurrence per line.
== Deleting ==
<code>sed -i '140,144d' /etc/named.conf</code> deletes the range of line numbers specified (140 through 144, in this case)


== References ==
== References ==

Latest revision as of 04:10, 2 March 2017

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). Sed is similar editors that script edits, but sed only makes one pass over the input(s), and thus is more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.


Commands

: label

# comment

{....} Block

= print line number

a \ Append

b label Branch

c \ change

d and D Delete

g and G Get

h and H Hold

i \ Insert

l Look

n and N Next

p and P Print

q Quit

r filename Read File

t label Test

w filename Write Filename

x eXchange

y/..../..../ - Transform

s/..../..../ - Substitute


sed Pattern Flags

/g Global. By default, sed only matches/replaces the first occurrence of a pattern for each line. The /g makes it replace all occurrences.
Example:
sed 's/red/blue/g' < file.txt


/I Ignore Case

/p Print

/w filename Write Filename

Special Characters

& Corresponds to the pattern found.

& is used when searching for a pattern and then adding some characters (such as parenthesis> around or near the pattern sed found. It's easy if you're searchin for a particular string:

sed 's/abc/(abc)/' <old.txt >new.txt - Replaces abc with (abc)

What if you don't know the output of the search string? You can use the special character &, it represents the pattern that sed found.

sed 's/[a-z]*/(&)/' <old.txt >new.txt


Command Line Options

-r   Enable sed to use Regular Expressions


-e The -e option lets you combine multiple commands

sed -e 's/a/A/' -e 's/b/B/' <old.txt >new.txt

-i Inline editing.


Substitution

s/..../..../ Substitute. Changes the 1st pattern to the second pattern.

Example:
sed 's/red/blue/' < file.txt Changes "red" to "blue" in file.txt
or:
sed -i 's/red/blue/' file.txt Changes "red" to "blue" in file.txt

    cat file.txt | sed 's/one/two/' Changes "one" to "two" in file.txt

The search pattern is on the left, and the replacement string is on the right. By default, sed replaces the first occurrence per line.


Deleting

sed -i '140,144d' /etc/named.conf deletes the range of line numbers specified (140 through 144, in this case)

References

  1. sed man page
  2. Grymoire sed tutorial