ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, you have to think at the two commands executed in sequence on every line of the input file: read first line, don't quit, delete, read second line, don't quit, delete.... read tenth line, quit. This means the first nine lines are deleted and when the 10th line is reached the program terminates and the line is printed out.
The same for the second example: every line before the 5th one is deleted and when the 5th line is reached the program terminates and it's printed out. The second quit command (45q) is irrelevant, since that line is never reached.
Hi.
This behaviour becomes pretty straightforward when you remember that sed reads each line into a buffer called pattern space, evaluates all given commands and, if no `-n' option was specified, prints its contents, in this order. So, for lines 1-9, it reads a line, then evaluates `10q', which has no effect on these lines, then evaluates `d', which clears pattern space, then it tries to print pattern space, which is empty, so nothing is printed. The same happens for lines 11, 12, etc. For line #10, `10q' makes sed to quit immediately (skipping all other commands), but before it quits the pattern space is printed.
It's ok, your reply does give more input to me about the pattern space logic, appreciated that too
With colucix explanation, I tried just now:
Code:
sed 'd;5q;45q' logfile
, it gives empty result, which means the d delete the pattern space before it even reaches to 5q. So using
Code:
sed 'd;5q;45q;10q' logfile
with the extra command behind d will give no meaning as the d in front already delete everything in the pattern space. Correct ?
And if we use
Code:
sed '45q;5q;10q;d' logfile
, the q will only processed at the 5th line and print it out, other lines below will not be processed so it make no sense to put more q inside a sed command. Am I right in understanding this ?
This q comes out to be a cool way in printing one particular line from a pretty large file. I tested just now compare with p and d:
Code:
sed -n '5p' logfile
and
Code:
sed '5!d' logfile
, the q way really give faster result with a huge file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.