Input file:
$ cat details.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
Required: Reverse the order of the lines from line5 to line8. i.e. required output:
line1
line2
line3
line4
line8
line7
line6
line5
line9
line10
The awk solution:
s=$0
for(i=from+1;i<to;i++){
getline;s=$0"\n"s
}
getline;print;print s
next
}1' details.txt
The 1 above in awk one liner can be replaced as {print}
In order to reverse the order of lines of the whole file, we have tac command, which print files in reverse.
$ tac details.txt
The same can be achieved using sed and awk as mentioned below:
$ sed -n '1!G;h;$p' details.txt
$ awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' details.txt
1 comment:
my weird way of doing this :
cat txt | sed -n '1,4p' && cat txt | tail -n 6 | head -n 4 | tac && cat txt | sed -n '9,10p'
Post a Comment