LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2012, 04:03 AM   #1
sylye
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Mandrake 9.1, Debian 3.1,Centos 5.x,6.x,Slackware 13.37,14.0,14.1
Posts: 49

Rep: Reputation: 1
sed q


hi,

Just came across someone using sed with the following way:

Code:
sed '10q;d' logfile
which will eventually print the line no.10 of the logfile. I have never thought it can be use that way. However I don't understand how it works.

I understand by using
Code:
sed '10q' logfile
will print the first 10 line and simulate the function of ''head''. But what does the ''d'' do that delete the previous 9 line ? What logic behind ?

I then tried with
Code:
sed '5q;45q;d' logfile
, it prints the 5th line of logfile. How does it work this time ?


Anyone would like to share ? Just for knowledge purpose, nothing specific function to achieve here.

Thanks for sharing though
 
Old 11-06-2012, 04:19 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 11-06-2012, 04:25 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.

PS: I'm late again.
 
2 members found this post helpful.
Old 11-06-2012, 04:56 AM   #4
sylye
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Mandrake 9.1, Debian 3.1,Centos 5.x,6.x,Slackware 13.37,14.0,14.1
Posts: 49

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by firstfire View Post
PS: I'm late again.
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.
 
Old 11-06-2012, 05:29 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes, all correct.

Thanks firstfire for the in-depth explanation!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Cygwin, sed] Using filenames as both files and search strings within sed lingh Linux - Newbie 5 10-20-2012 10:38 AM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
sed and regexp matching (GNU sed version 4.2.1) Ashkhan Programming 8 02-27-2012 09:12 AM
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration