Consider the file
$ cat name.txt
1 k
2 ka
3 kam
4 kama
5 kamar
6 kamara
7 kamaraj
8 kamaraju
9 kamaraju
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi
To print the contents of lines between 5 and 8
$ sed -n '5,8p' name.txt
5 kamar
6 kamara
7 kamaraj
8 kamaraju
$echo $?
0
To print the lines 10 to 20, just do
$ sed -n '10,20p' name.txt
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi
$echo $?
0
Note that this command is successful even though the specified right limit (20) is greater than the number of lines in the file (19).
To print the nth line of a file (w.l.g. say n=8), just do
$ sed -n 8p name.txt
8 kamaraju
$ echo $?
0
End of line is represented by $. So, to print from 17th line till the end of the file, do
$ sed -n '17,$p' name.txt
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi
$ echo $?
0
tags | print specific lines in a file
$ cat name.txt
1 k
2 ka
3 kam
4 kama
5 kamar
6 kamara
7 kamaraj
8 kamaraju
9 kamaraju
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi
To print the contents of lines between 5 and 8
$ sed -n '5,8p' name.txt
5 kamar
6 kamara
7 kamaraj
8 kamaraju
$echo $?
0
To print the lines 10 to 20, just do
$ sed -n '10,20p' name.txt
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi
$echo $?
0
Note that this command is successful even though the specified right limit (20) is greater than the number of lines in the file (19).
To print the nth line of a file (w.l.g. say n=8), just do
$ sed -n 8p name.txt
8 kamaraju
$ echo $?
0
End of line is represented by $. So, to print from 17th line till the end of the file, do
$ sed -n '17,$p' name.txt
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi
$ echo $?
0
tags | print specific lines in a file
No comments:
Post a Comment