How to Use SED Unix Command

In this tutorial we will show you how to use the SED command in a Unix environment. The sed unix command is easy to use and work with in Linux.
Amongst the unix commad sed allows you to work with the text in a file. Follow the tutorial below to learn more.



Step 1 – List the directory contents
The SED command can be used to transform basic text data.To look at an example, first let’s list the contents of the directory here.
List the contents

Step 2 – Find out the number of lines in a text file
If you want to check how many lines are present in a text file, for that, you can type:
sed –n ‘$=’ text.txt
You can see that this particular text file over here contains 12 lines.
Figure out the number of lines in a text file

Step 3 – View data in reverse order
If you want to view your text file data in reverse order, type:
sed ‘1!G;h;$!d’ text.txt and hit enter
With that done, you can see that the entire data is being displayed in reverse order.
Look at the data in reverse order

Step 4 – Enter the SED command
Now let’s use the sed unix command to do multiple functions using a single line operation. Over here, we will add double quotes in the text file and duplicate the content in a new text file.
For that, type in:
sed ‘s/.*/“&”/g’ text.txt > text-output.txt and hit enter.
Type in the SED command

Step 5 – Check the data
To double check, let’s use the cat command to check the data in the new text file.
And over here, you can see that through the unix commands of sed the data has been successfully altered and duplicated in the new file.
Check data using the “cat” command