Sed: Difference between revisions

From Psygen Wiki
Jump to navigation Jump to search
No edit summary
Line 36: Line 36:
<code>r <i>filename</i></code> Read File
<code>r <i>filename</i></code> Read File


'''Substitution:'''
'''Substitution:''' <br />
<code>s/..../..../</code> Substitute. Changes the 1st pattern to the second pattern.<br />
<code>s/..../..../</code> Substitute. Changes the 1st pattern to the second pattern.<br />


Line 42: Line 42:


&nbsp; &nbsp; <code>cat file.txt | sed 's/one/two/'</code> Changes "one" to "two" in file.txt
&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.




Line 58: Line 60:
<code>/p</code> Print
<code>/p</code> Print
<code>/w <i>filename</i></code> Write Filename
<code>/w <i>filename</i></code> Write Filename
== Special Characters ==
<code>&</code> Corresponds to the pattern found.
== Command Line Options ==
<code> -r </code> Enable sed to use [[Regular Expressions]]





Revision as of 18:31, 10 January 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

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

    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.


t label Test

w filename Write Filename

x eXchange

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

sed Pattern Flags

/g Global /I Ignore Case /p Print /w filename Write Filename


Special Characters

& Corresponds to the pattern found.

Command Line Options

-r Enable sed to use Regular Expressions


References

  1. sed man page
  2. Grymoire sed tutorial