Actions

Sed: Difference between revisions

From Jedisaber Wiki

No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 86: Line 86:


== Substitution ==
== Substitution ==
'''Replace a word with another word in a line:'''


<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 />


'''Example:'''<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 />
<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.<br />
By default, sed replaces the first occurrence of the word on each line.
 
'''If you want to replace all occurrences of the word on each line, add the g flag:'''
 
<code>sed -i 's/red/blue/g' sample.txt</code>


The search pattern is on the left, and the replacement string is on the right. By default, sed replaces the first occurrence per line.
 
'''Replace a whole line that matches the pattern:'''
 
<code>sed -i '/Pattern/c\Replacement line here' sample.txt</code>
 
 
''' Replacing text in multiple files at once:'''
 
For an example, let's say we want to replace http: with https: in all the .html files in a directory:
 
<code>sed -i 's/http:/https:/g' *.html</code>
 
 
'''Remove leading and trailing whitespace:'''
 
<code>sed -i 's/^[ \t]*//;s/[ \t]*$//' sample.txt</code>
 
 
== Inserting ==
 
'''Add a line of text before the pattern match:'''
 
<code>sed -i '/Pattern/i Before this line' sample.txt</code>
 
- "Pattern" is what to search for.<br />
- i is for Insert<br />
- "Before this line" is the text that will be inserted before the line that has the pattern match
 
'''Add a line after the pattern match:'''
 
<code>sed -i '/Pattern/a Afterthis line' sample.txt</code>




Line 102: Line 137:


<code>sed -i '140,144d' /etc/named.conf</code> deletes the range of line numbers specified (140 through 144, in this case)
<code>sed -i '140,144d' /etc/named.conf</code> deletes the range of line numbers specified (140 through 144, in this case)
'''Delete empty lines:'''
<code>sed -i '/^$/d' sample.txt</code>
'''Delete a specific line:'''
<code>sed -i '3d' sample.txt</code> - Deletes line 3 from the file, sample.txt


== References ==
== References ==
Line 108: Line 153:
   <li>[http://man.he.net/?topic=sed&section=all sed man page]</li>
   <li>[http://man.he.net/?topic=sed&section=all sed man page]</li>
   <li>[http://www.grymoire.com/Unix/Sed.html Grymoire sed tutorial]</li>
   <li>[http://www.grymoire.com/Unix/Sed.html Grymoire sed tutorial]</li>
  <li>https://pro.tecmint.com/blog/sed-command-examples/</li>
</ol>
</ol>

Latest revision as of 18:12, 27 June 2025

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

Replace a word with another word in a line:

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

Example:
sed -i 's/red/blue/' file.txt Changes "red" to "blue" 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 of the word on each line.

If you want to replace all occurrences of the word on each line, add the g flag:

sed -i 's/red/blue/g' sample.txt


Replace a whole line that matches the pattern:

sed -i '/Pattern/c\Replacement line here' sample.txt


Replacing text in multiple files at once:

For an example, let's say we want to replace http: with https: in all the .html files in a directory:

sed -i 's/http:/https:/g' *.html


Remove leading and trailing whitespace:

sed -i 's/^[ \t]*//;s/[ \t]*$//' sample.txt


Inserting

Add a line of text before the pattern match:

sed -i '/Pattern/i Before this line' sample.txt

- "Pattern" is what to search for.
- i is for Insert
- "Before this line" is the text that will be inserted before the line that has the pattern match

Add a line after the pattern match:

sed -i '/Pattern/a Afterthis line' sample.txt


Deleting

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


Delete empty lines:

sed -i '/^$/d' sample.txt


Delete a specific line:

sed -i '3d' sample.txt - Deletes line 3 from the file, sample.txt

References

  1. sed man page
  2. Grymoire sed tutorial
  3. https://pro.tecmint.com/blog/sed-command-examples/