Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Saturday, January 9, 2021

Linux Series - SED Utility/Package - 2. Input Methods

As mentioned in this article, sed reads the input from two different ways.

1. via file

   Example: Let us create a file called test.txt and it contains the text called Linux.


This file is an input to sed command. Let us see how sed process this input file.
Run the command "sed 's/Linux/Windows/' test.txt". 

Basically, option "s" passes in sed command when you want to substitute the text with another text.  "s" means substitution
sed reads the input of test.txt file and it replaces the word "Linux" with "Windows" and displayed in the terminal.  However, this above command will not replace "Linux" with "Windows" in actual file (test.txt).  Let us confirm it.

2. via pipe
Hope you know how pipe works in linux. If not, the below picture represents how pipe works.
Output of command1 will be the input for command2.
Example: Let us use pipe in sed command to understand how it works. For this, we are again using the same test.txt file.

When you execute "cat test.txt" command will return the text "Linux" which is the input to sed command where sed replaces "Linux" with "Windows".

Hope you understand how sed reads the input and process the command😀.





Linux Series - SED Utility/Package - 1. Installation

When you are working in Linux, you might have heard the term called 'SED' utility. Even if you don't, it is always good to know SED utility which makes your job easier when you are manipulating text.

'sed' stands for stream editor.  

  • It is a powerful text stream editor and can perform insertion, deletion, search and replace(substitution) on the input.
  • It supports regular expression which allows to perform complex pattern matching.

SED reads a line from the input stream (file, pipe, or stdin) and display the outcome in stdout. Also, it can update the output in the file as well when the option 'i' is passed along with sed command. We can cover this later in the series)

Lets stop theoretical definitions and go on for hands on to understand more about sed utility.

First job is to check sed package installed in your linux environment. To do so,

run the command "sed --version"

As you see, sed package is not installed in my environment, you need to install it.
To do so, run the command "sudo apt-get install -y sed" (Make sure you are running the command with super user access and you need to run "sudo yum install -y sed" if you are using centos linux distribution)


Now let us run the command (sed --version) to check sed package installed in my linux environment.


This is how you need to make sure the installation of sed package in your linux environment.

Hope you understand now 😀. 




Linux Series - SED Utility/Package - 4. Deleting Lines

Let’s explore sed’s explicit delete command, which you specify by using the option 'd'. Again, we are using the sample file named ...