sed h and g options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed h and g options
# 1  
Old 01-04-2006
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
# 2  
Old 01-04-2006
Have you tested it yourself?

Cheers
ZB
# 3  
Old 01-04-2006
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
# 4  
Old 01-04-2006
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 04:39 AM..
# 5  
Old 01-04-2006
Bug

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
# 6  
Old 01-04-2006
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.
# 7  
Old 01-04-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Sed error - multiple number options to `s' command

Hi All, I am having two files (file1 & file2) and a filelist.txt file below. file1: $$STRINGVAR1=5 $$STRINGVAR2=10 $$LAST_UPD_DT_TBL1=12/12/2010 12:00:00 $$STRINGVAR3=100 $$LAST_UPD_DT_TBL2=01/01/2010 12:00:00... (8 Replies)
Discussion started by: Chandru_Raj
8 Replies

2. Ubuntu

Kernel boot options removed by fault, no boot options

Hello Everyone, First of all, I highly appreciate all Linux forum members and whole Linux community. http://forums.linuxmint.com/images/smilies/icon_wink.gif. I wish you the best for all of you ! I will try to be short and concise: I am using Linux Mint 10 for 2 months on 2 ws, and all went... (3 Replies)
Discussion started by: cdt
3 Replies

3. Shell Programming and Scripting

cut, sed, awk too slow to retrieve line - other options?

Hi, I have a script that, basically, has two input files of this type: file1 key1=value1_1_1 key2=value1_2_1 key4=value1_4_1 ... file2 key2=value2_2_1 key2=value2_2_2 key3=value2_3_1 key4=value2_4_1 ... My files are 10k lines big each (approx). The keys are strings that don't... (7 Replies)
Discussion started by: fzd
7 Replies

4. Shell Programming and Scripting

Sed options

I have a file with name input_file.spec. This file has records like: Record #: 1 rec_len = 590 rec_id = 31229 filler_4 = " " orig_id = 902162988 seqnum = 138960799 lrnid ... (3 Replies)
Discussion started by: Ajay_84
3 Replies

5. UNIX for Advanced & Expert Users

Use of SED in Perl or any other options

Hi Folks, I am working on a data extraction script in perl.I am a begineer in perl and shell scripting. So i need your inputs to solve the script problem. I have data coming from database in the format :- <b> _ABC (Apply Changes) (p) </b> _EFG | HIJ | KLM | WER <b> _E3R (Apply Changes)... (3 Replies)
Discussion started by: subhotech
3 Replies

6. AIX

no options

Hi All, I have a situation here that's very fun... I have a system with AIX and iPlanet (sunOne) installed, when occurs an unknown event on the network the WebServer shows a thousand of CLOSE_WAIT connections and this number grows and grows until the webserver crashs. I read some documents... (2 Replies)
Discussion started by: nascimento.rp
2 Replies

7. UNIX for Dummies Questions & Answers

options

I am just beginning to learn unix and I was wondering if there was a list of all the options somewhere on the net or hidden in the man pages? Also do options always have - and then a letter, or can it be - and a number as well? Thanks! (1 Reply)
Discussion started by: terms5
1 Replies

8. UNIX for Dummies Questions & Answers

cp options

Hello again, Is there an option for the cp command to overwrite existing files in the destination directory? Cheers Rob (3 Replies)
Discussion started by: milage
3 Replies
Login or Register to Ask a Question