简体   繁体   中英

Is there a way to print lines from a file from n to m and than reverse their positions?

I'm trying to print text from line 10 to 20 and then reverse their positions.

I've tried this:

sed '10!G;h;$!d' file.txt

But it only prints from 10 to end of the file. Is there any way to stop it at line 20 by using only one sed command?

Almost there, you just need to replace $!d with the 'until' line-number

sed -n '10,20p' tst.txt
// Prints line 10 <--> 20
sed -n '10!G;h;20p' tst.txt
// Prints REVERSE line 10 <--> 20

output:

20
19
18
17
16
15
14
13
12
11
10

tst.txt:

1
2
3
4
...
19
20

Info

You can use this to print a range of lines:

sed -n -e 10,20p file.txt | tac

tac will reverse the order of the lines

对于那些没有TAC的人(例如那些Mac用户):

sed -n -e 10,20p file.txt | tail -r 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM