I was having query regarding a command posted in this question
As I understand the sed flow
sed -n '1!G;h;$p'
Sed flow occurs left to right in loop on every single line. Means every single line in input it will try to execute every single commands specified by semicolon. In above example, --sed reads first line into pattern space ---Now it has three sets of commands it will try to execute each on line ---> It reads first command that is 1!G it will try to execute it on line but since the line currently read is first and negation is supplied it will skip to second command, then it will try to execute third command which is $p but as third command is to print last line it will be skipped for all consecutive lines until the last line.
If I am right about my above understanding, then for below command
sed -n '1!G;h;7p;8p'
When 8th line is read it should print 7th and 8th line, printing command should not be applied to any other line.
But it was printing 15 lines in reverse order. It was printing 1-8 lines and then again 1-7 lines. Can anyone help clarify.
As per my undetstanding and sed documentation sed operates line by line in left to right order *but above command seems to process entite file again
What was occuring was instead of printing 1to 8 line it was storing 7 lines in patten space printing them then reading 8th linr and printing pattern space again.
Based on above observation sed acts on pattern space for each line read.
Then For below command
sed -n '1!G;h;7p'
Since commands are executed in order, Print command should be executed only when 7th line is read and it should printed but it was printing 1-7 line of patterm space
Hence 7p should literally mean print 7th line But Here it act as range, Means if pattern space has 7th line of input then print the 1-7.
sed '7p' -n ---> Prints 7th line
sed -n '1!G;h;7p'---> Prints 1-7line.
sed -n '1!G;h;1,7p' ---> Prints 1, 1-2, 1-3, 1-4, 1-5, 1-6, 1-7 lines
Can someone clarify why it was occuring?