Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 6: Learning expressions with ex
Next

Printing text on-screen

First open the our demofile as follows:
$ ex demofile
"demofile" [noeol] 20L, 387C
Entering Ex mode. Type "visual" to go to Normal mode.

Now type 'p' in front of : as follow and press enter
:p
Okay! I will stop.
:

NOTE By default p command will print current line, in our case its the last line of above text file.

Printing lines using range

Now if you want to print 1st line to next 5 line (i.e. 1 to 5 lines) then give command
:1,5 p
Hello World.
This is vivek from Poona.

I love linux.
It is different from all other Os

NOTE Here 1,5 is the address. if single number is used (e.g. 5 p) it indicate line number and if two numbers are separated by comma its range of line.

Printing particular line

To print 2nd line from our file give command
:2 p
This is vivek from Poona.

Printing entire file on-screen

Give command
:1,$ p
Hello World.
This is vivek from Poona.

I love linux.
It is different from all other Os

.....
...
.....

Okay! I will stop.

NOTE Here 1 is 1st line and $ is the special character of ex which mean last-line character. So 1,$ means print from 1st line to last-line character (i.e. end of file). Here p stands print.

Printing line number with our text

Give command
:set number
:1,3 p

1 Hello World.
2 This is vivek from Poona.
3

NOTE This command prints number next to each line. If you don't want number you can turn off numbers by issuing following command
:set nonumber
:1,3 p

Hello World.
This is vivek from Poona.


Prev
Home
Next
Getting started with ex
Up
Deleting lines