The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
no options nascimento.rp AIX 2 08-20-2006 11:46 AM
options terms5 UNIX for Dummies Questions & Answers 1 01-26-2006 10:50 PM
options with awk mips Shell Programming and Scripting 2 05-15-2004 12:33 AM
cat and wc options Laura01 Shell Programming and Scripting 1 09-08-2002 08:21 PM
cp options milage UNIX for Dummies Questions & Answers 3 07-12-2001 09:20 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-04-2006
Registered User
 

Join Date: Jan 2006
Posts: 14
Question sed h and g options

Hi ..

Happy New Year..

If i do a

sed -e '
/BEGIN/,/END/{
h
/xxx/{
g
p
}
}
' temp1

I have two queries in this script

1.Will the block between BEGIN and END be stored in the hold buffer before executing the /xxx/ ..

2.When will /xxx/ be executed ..( Is it after one full block between BEGIN and END is completed ?)

temp1 looks like

BEGIN
sdfdsf
sdfsdf
xxx
END
BEGIN
sdfdsf
sdfsdf
END


Thanks..Siva
Reply With Quote
Forum Sponsor
  #2  
Old 01-04-2006
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Have you tested it yourself?

Cheers
ZB
Reply With Quote
  #3  
Old 01-04-2006
Registered User
 

Join Date: Jan 2006
Posts: 14
sed h and g options

Yes I tested it myself ..

My objective is to take the block which has only xxx in it

But it gives output like

BEGIN
sdfdsf
sdfsdf
xxx
xxx
END
BEGIN
sdfdsf
sdfsdf
END

.. I am pretty new to sed .. may be my script has some basic error .. please help
Reply With Quote
  #4  
Old 01-04-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
This looks fine.. It makes use sed's advanced flow control commands

Code:
[/tmp]$ cat 1
BEGIN
sdfdsf
sdfsdf
xxx
END
BEGIN
sdfdsf
sdfsdf
END
[/tmp]$ sed -n -e '/BEGIN/{
:top
N
/END/b end
b top
:end
s_.*xxx.*_&_p
}' 1
BEGIN
sdfdsf
sdfsdf
xxx
END
[/tmp]$
Yet another similiar sed solution
Code:
sed -n -e '/BEGIN/{
:top
/END/!{
N;        
b top
}    
s_.*xxx.*_&_p
}' 1
Cheers'
Vino

Last edited by vino; 01-18-2006 at 01:39 AM.
Reply With Quote
  #5  
Old 01-04-2006
Registered User
 

Join Date: Jan 2006
Posts: 14
Smile

Thanks Vino

It works ..

But what if I need the block with two patterns in it ..( the order of occurence of the pattern is not known ..

Could you help me do this using hold and get options in sed ..
I am a beginner in sed and I would love to learn using h and g options ..
( as I had asked in my original question .. /BEGIN,/END/h .. will the block be stored in hold space ??)

Thanks
Siva
Reply With Quote
  #6  
Old 01-04-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by sivasenthil_k
But what if I need the block with two patterns in it ..( the order of occurence of the pattern is not known ..
Give me an example.

Quote:
Originally Posted by sivasenthil_k
( as I had asked in my original question .. /BEGIN,/END/h .. will the block be stored in hold space ??)
Sed works as follows: Pick up a line. Do the specified operations on that. Read the next line. Do the specified operations... etc.. So, in your case, the first line with /BEGIN/ will be in the hold space.
Reply With Quote
  #7  
Old 01-04-2006
Registered User
 

Join Date: Jan 2006
Posts: 14
Hi Vino

Thanks for the reply

Example is ..

Say I need the blocks with xxx and yyy in the file below ( I need the first and last blocks to be in the output .. which has xxx and yyy )

BEGIN
aaa
yyy
bbb
ccc
xxx
END
BEGIN
aaa
yyy
bbb
ccc
END
BEGIN
aaa
xxx
bbb
ccc
END
BEGIN
aaa
xxx
bbb
ccc
yyy
END
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0