Understanding sed – Practical Guide

Sed is a powerful stream editor, typically used for editing large amounts of data by providing a simple command. Sed is also used for sophisticated searches, where the Regular Expressions are used.

You can use sed to:

  • Automate editing actions to be performed on one or more files
  • Simplify the task of performing the same edit on multiple files
  • Converting one data format to another

For example the following command:

Do the following:

  • Edit the file in place
  • Delete lines started with #  (/^#/d)
  • Delete blank lines (/^$/d)
  • Create a backup file with bup extension

To learn sed , we need to understand the syntax and learn by examples

Print command

Print all file

The above displays every line twice because it dump the file content to stdout while read it, to remove the stdout add –n:

Print lines 1-5

(if we remove the –n flag we get all file output to stdout but only the matched line will displayed twice)

Find line started with ‘root’ and print it

Print from line 15 to the end:

Print all except from line 15 to the end:

print lines start with ‘a’ followed by number

print lines with exactly 3 characters

 

Substitute – Search and replace

General syntax 

simple example:

search for lines started with root and replace the first occurrence with ‘boot’

do it for all occurrences

 

search and replace default shell for user root

note that we changed the delimiter because we want to use / in the search term

so this will also works:

 

The range can be regular expression or lines :

Add -n  to suppress the output and p to display the lines affected

Add -i (without p) to make the changes in place (edit the file)

Add -i.bak to make changes and save the original for backup with bak extenstion

 

Transform Example:

Transform any ‘a’ to ‘_’ and any ‘I’ to ‘*’

 

Insert, Append and delete

Add line ‘hello’ before lines 2-4:

Add line ‘hello’ after lines 2-4:

Add line ‘hello’ after each line started with s

Delete lines 2-4

Delete lines starting with s

more regular expressions:

Delete blank lines:

 

Multiple sed expressions:

On the command line type:

write in a file and use -f to run it:

in place:

 

Substitution Groups

Example:

Lets explain it step by step:

We define groups with ( ) but we need to add escape chars it so it should be written ‘\(\)’

[^,] – anything except comma (,)

[^,]* – zero and more anything except comma

so it should be written like this:

replace string:

\U – command to uppercase

\1 – the first group

So this example uppercase the first field in CSV file:

 

It looks better if we use different command separator:

 

using more groups:

Run the substitution 

Change order

 

Executing commands

replace the starting line with ‘ls -l ‘

execute the commands:

 

 

 

 

Tagged

Leave a Reply

Your email address will not be published. Required fields are marked *