Example 1: Display specific lines (based on line number) of a file using sed command
View only the specific lines mentioned by line numbers.
Syntax:
$ sed -n -e Xp -e Yp FILENAME
sed : sed command, which will print all the lines by default. -n : Suppresses output. -e CMD : Command to be executed Xp: Print line number X Yp: Print line number Y FILENAME : name of the file to be processed. The example mentioned below will print the lines 120, 145, 1050 from the syslog.
$ sed -n -e 120p -e 145p -e 1050p /var/log/syslog
In the following example, you can view the content of var/log/cron from line number 101 to 110.
M – Starting line number N – Ending line number
Syntax: sed -n M,Np FILENAME
$ sed -n 101,110p /var/log/cron