Wednesday, 3 February 2016

Basic print examples in Unix using Sed

I was long thinking to write a short tutorial on covering some basic Sed examples for easy and quick reference. So here we are. In this short post I'll try to include some basic print examples with some variety... the more the merrier right?

The sample Testfile.txt we'll be using in this example is as below :

1.Here is a cute little tutorial for
2.trying out printing things using
3.the Stream Editor or Sed in Unix.
4.The author of this tutorial is
5.none other than IroncladWriter
6.himself. The tutorial aims to
7.teach folks and educate how
8.they could unlock the power
9.of Unix to the fullest. Am posting
10.this on the blog IronCladZone
11.The best blog on the whole of
12.internet, where you can learn
13.technical stuff as well enjoy 
14.entertaining tidbits like movies,
15.music, television, fashion, food,
16.shopping, travel, trends etc.
17.Just sit back, relax and

18.ENJOY THE RIDE :)

* Print only the first line of a file

sed -n '1p' Testfile.txt

OR you could also use the following:

sed q Testfile.txt

* Print only the last line of a file

sed -n '$p' Testfile.txt

* Print only the lines between 8th and 11th line of a file

sed -n '8,11p' Testfile.txt

* Print only those lines having the word "Unix" from the file

sed -n '/Unix/'p Testfile.txt

* Print only those lines after the word/pattern "shopping" to the end of the file. Note this will include the line containing the word "shopping" as well.

sed -n '/shopping/,$p' Testfile.txt

In my next post, I'll try to cover some advanced printing examples using Sed. Stay tuned fellas.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker