Editing output using sed

The sed command is a non-interactive stream editor that allows you to modify the content of the standard input or file. It performs an operation on each line in a pipeline. The syntax will be:

sed [OPTIONS]... {script} [input-file …]

By default, the output is displayed on stdout, but can be redirected to a file if specified.

The input-file are the files on which sed needs to be run. If no files are specified, it reads from stdin.

The script can be a command or a file with multiple commands to pass to sed, and OPTIONS to sed are described in the following table:

Option

Description

-n

This suppresses automatic printing of pattern space

-e script

This allows multiple scripts to be executed

-r

This uses the extended regex in the script

-l N

This specifies line wrap length

--posix

This disables all GNU extensions

-u

This loads the minimal amounts of data from input and flushes output buffers frequently

String substitution using s

The sed command is widely used for string substitution in a text file. Programmers frequently use this feature while renaming a variable in a huge source code. It saves a lot of programmers' time by avoiding manual renaming.

The substitution command s has the following field:

s/regex/replacement/

Here, s means perform substitution, / acts as separator, and regex is a regular expression that needs to be replaced. A simple string can also be specified here. The last field replacement is with what matched results should be replaced.

By default, sed will replace only the first occurrence of a matched pattern in a line. To replace all occurrences, use the g flag after the end of /—, that is, s/regex/replacement/g.

Some of the flags that can be used are mentioned in the following table:

Flag

Description

g

This applies replacement to all the matches in a line

p

This prints a new pattern space, if substitution occurs

w filename

This writes substituted pattern space to a filename

N

This replaces only the Nth matched result in a matched line

We have the sed.sh file for our example. The content of this file is as follows:

$ cat sed.sh
#!/bin/bash

var1="sed "
var1+="command "
var1+="usage"

echo $var1

This is a shell script, where the variable var1 has been used at four places. Now, we want to rename the variable var1 to variable. We can do this very easily using the sed command:

$ sed -i 's/var1/variable/g' sed.sh
$ cat sed.sh
#!/bin/bash

variable="sed "
variable+="command "
variable+="usage"

echo $variable

Here, the -i option is used to replace an input file.

Multiple substitutions

We can also specify multiple commands to be executed for substitution using -e followed by a command.

For example, consider the sed.txt file. The content of this file is as follows:

$ cat sed.txt
The sed command is widely used for string
substitution in text file. Programmers frequently
use this feature while renaming a variable in huge source code.
It saves lot of programmers time by avoiding manual renaming.

Now, we want to replace '.' with ',' and delete the line containing a string manual:

$ sed -e 's/./,/g' -e '/manual/d' sed.txt
The sed command is widely used for string
substitution in text file, Programmers frequently
use this feature while renaming a variable in huge source code,

In sed.txt file, the s/./,/g command first replaces '.' with ',' and /manual/d deletes further the line containing the string manual.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.2.48