Stream Editor Useful Recipes - Sed (Stream Editor)

What are Useful Recipes in Stream Editor?

SED is an amazing utility that allows multiple ways to solve a problem. SED perfectly proves this UNIX way. Many useful utilities are provided by GNU/LINUX to perform day-to-day tasks. A few utilities using SED are simulated as follows. It’s just to demonstrate the power of SED but it appears as solving an easy problem the hard way.

Cat Command

Each line is printed as a part of the default workflow in the following code.

The following result is obtained on executing the above code:

The print command is used to display the file contents.

The following result is obtained on executing the above code:

Removing Empty Lines

This "^$" implies empty line, and empty lines are deleted when a pattern match succeeds.

The following result is obtained on executing the above code:

The following example prints the line only when it is non-empty.

The following result is obtained on executing the above code:

Removing Commented Lines from a C++ Program

A sample C++ program is created.

The following regular expressions are used to remove the comments.

The following result is obtained on executing the above code.

Adding Comments Before Certain Lines

The below code adds comments before line numbers 3 to 5.

The following result is obtained on executing the above code:

Wc -l command

The "wc -l" command counts the number of lines present in the file. The following SED expression simulates the same.
[jerry]$ sed -n '$ =' hello.sh

On executing the above code, you get the following result:

Head Command

The head command prints the first 10 lines of the file. The same behavior is simulated with SED.

The following result is obtained on executing the above code:

Tail -1 Command

The "tail -1" prints the last line of the file. The simulation is showed in te below code.

The following result is obtained on executing the above code:

The SED script is written as follows.

The following result is obtained on executing the above code:

Dos2unix Command

In DOS environment, a newline is represented by a combination of CR/LF characters. The simulation of "dos2unix" command converts a DOS newline character to UNIX newline character. In GNU/Linux, this is treated as "^M" (Control M) character.

The following result is obtained on executing the above code:

The command using SED is simulated as follows.

The following result is obtained on executing the above code:

The file contents are displayed as follows.

The following result is obtained on executing the above code.

Unix2dos command

"dos2unix" and "unix2dos" are similar and they use the command which converts UNIX newline character to DOS newline character.

The following result is obtained on executing the above code:

The command using SED is simulated as follows.

The following result is obtained on executing the above code:

The file contents are displayed as follows.

The following result is obtained on executing the above code:

Cat -E command

The end of the line is shown by "cat -E" command by Dollar($) character. The following SED example is simulation of the same.

The following result is obtained on executing the following code:

The command using SED is simulated as follows.

The following result is obtained on executing the above code:

Cat -ET Command

The "cat -ET" command shows the Dollar($) symbol at the end of each line and displays the TAB characters as "^I". The below example shows the +simulation of "cat -ET" command using SED.

The following result is obtained on executing the below code:

The command using SED is simulated as follows:

The following result is obtained on executing the above code:

nl Command

The "nl" command simply numbers the lines of files. The SED script simulates this behavior.

The following result is obtained on executing the above code:

The first SED expression prints line numbers followed by their contents, The second SED expression merges the two lines and converts newline characters to TAB characters.

cp Command

The "cp" command creates another copy of the file. This SED script simulates this behavior.

The following result is obtained on executing the above code.

Expand Command

TheTAB character is converted into whitespaces using "expand" command. The following code shows its simulation.

The following result is obtained on executing the above code:

Tee Command

The "tee" command dumps the data to the standard output stream as well as file. The simulation of the "tee" command is simulated as follows:

The command using SED is simulated as follows:

The following result is obtained on executing the above code:

cat -s Command

UNIX "cat -s" command suppresses repeated empty output lines. The simulation of "cat -s" command is shown as follows:

The following result is obtained on executing the above code:

The command using SED is simulated as follows:

On executing the above code, the following result is obtained:

grep Command

The "grep" command prints a line by default when a pattern match succeeds. The following code shows its simulation.

On executing the above code, the following result is obtained:

The command using SED is simulated as follows.

The above code is executed and the following result is obtained:

grep -v Command

The "grep -v" command prints a line by default when a pattern match fails. The following code shows its simulation.

The above code is executed and the following result is obatined:

Simulate the command using SED.

The above code is executed and the following result is obtained:

tr Command

The "tr" command translates characters. Its simulation is shown below.

The above code is executed and the following result is obtained.

Simulate the command using SED.

The above code is executed and the following result is obtained:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Sed (Stream Editor) Topics