LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-23-2012, 11:13 PM   #1
Karljoe2
LQ Newbie
 
Registered: Apr 2012
Distribution: RHEL ,CentOS,Fedora
Posts: 7

Rep: Reputation: Disabled
Sed hold buffer


Hi team,


sed -ne 's/regex/{h;p}' <file.txt>

The above prints the whole file(and not just the searched regex).How does the 'h' and 'p' gets resolved in this case?
 
Old 12-23-2012, 11:17 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You are missing the RHS with the replacement expression. If you want to hold & print on a match, use:
/regex/{h;p}. I assume you need to do more as well, since just to print, you don't need to hold the line as well.

Last edited by jschiwal; 12-23-2012 at 11:18 PM.
 
Old 12-23-2012, 11:43 PM   #3
Karljoe2
LQ Newbie
 
Registered: Apr 2012
Distribution: RHEL ,CentOS,Fedora
Posts: 7

Original Poster
Rep: Reputation: Disabled
Ah, thats right(I actually am using ,/regex/{h;p}. My text file is below:

Linux
AIX
Windows
Mac

So when I use sed -ne '/Linux/{h;p}', it prints the whole file and not just the "Linux".why?
 
Old 12-24-2012, 05:07 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
No, it doesn't
Code:
markus@samsung:~/test$ cat file.txt 
Linux
Windows
Aix
Mac
markus@samsung:~/test$ sed -ne '/Linux/{h;p}' <file.txt 
Linux
It works as you expected.

Markus
 
Old 12-24-2012, 07:26 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The 'h' command copies the matching lines into the hold buffer, then the 'p' command prints the line. But you never do anything with the hold buffer after you copy to it, so it's as if the 'h' command doesn't exist.

You have to include another expression that moves the contents of the hold buffer back into the pattern buffer for it to have any use.

If you haven't seen it yet, the grymoire tutorial has a good lineup on multi-line use:
http://www.grymoire.com/Unix/Sed.html#uh-47

(Frankly, I've never been able to fully understand the use of the hold buffer though. I get the concept, but getting the flow control to come out correctly always seems to hang me up.)
 
1 members found this post helpful.
  


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
How does the sed buffer space work? ShanxT Programming 5 10-11-2011 10:53 AM
[SOLVED] SED Help (Pattern Buffer Overflow I think?) NvrBst Linux - Newbie 8 01-13-2010 05:56 PM
getting sed to hold lines and print them out again- help please inonzi_prowler Linux - Software 3 02-21-2007 03:26 AM
Sed gives output in pipes only when buffer is full jahvascriptmaniac Linux - Software 2 08-31-2006 05:57 PM
sed and its hold buffer???????? 3inone Programming 2 05-07-2004 05:56 AM

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

All times are GMT -5. The time now is 04:59 PM.

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