SED interval specification
Hi all
input Code:
BEGIN desired output Code:
I tried that Code:
cat begendf | sed '/^BEGIN/,/^END/ s/BEGIN/START/; s/END/EXIT/' Also tried some weird tricks using new spaces but neither working. Thanks a lot. |
Hi,
This: s/BEGIN/START/; s/END/EXIT/, will find all (!!) BEGIN and END lines, including the ones in the range. The scond part (all after the ; ) is not a part of the range specified in the first part, try this: sed -e '/^BEGIN/,/^END/s/ BEGIN/ START/' -e '/^BEGIN/,/^END/s/ END/ EXIT/' infile 2 sed actions (made possible by the -e options), first one for the BEGIN/END part, second for the END/EXIT part. Both are bound to a specific section. You could also rewrite it to: sed '/^BEGIN/,/^END/{s/ BEGIN/ START/;s/ END/ EXIT/}' infile All between the curly brackets ({...}) is bound by the section part. Hope this helps. |
druuna thanks a lot this really helps,
I saw lot of sed examples including -e option also read man but it was still unclear what is it for, if it works without it :) Now I know I should use it if I have multiple actions. I am wondering to one thing, in example that you've posted you replace s/ END/ EXIT/ and s/ BEGIN/ START/ that works because of whitespace (space in this case) before matching string. But is possible to exclude somehow the boundaries, from replacement ? So I could rewrite it to s/END/EXIT/ and s/BEGIN/START/ and still be working. Thanks |
I have done this through a dirty way :D. I am calling this dirty as this could have been done through sed/awk one liners like druuna has done, BUT my knowledge is limited to sed/awk.
Anyways, here is my script. I am assuming your file is named as file. And your desired output would be in file2. Code:
> /tmp/file1 |
Quote:
1. temporarily replace 'BEGIN' and 'END' that start a line by a unique string 2. replace all other occurences of 'BEGIN' and 'END' 3. restore the original values of the replacements in 1. e.g. Code:
<your input> | sed 's?^BEGIN$?__@@@__?' | sed 's?^END$?__///__?' | \ |
Quote:
|
Quote:
I'm not sure why you need to get rid of the space, it seems to be the simplest solution. |
hi vikas027
Thank you for you reply, I am staring at your script (cause I am beginner in bash scripting) and cannot figure out what is $? in [ $? -eq 1 ] and [ $? -eq 0 ] I know that -eq 1(0) is condition testing for equaling to 1(0) but what is $? :) ? vonbiber: Interesting solution, thanks I had to a little change code to to what I want but preserves your idea ;) Code:
cat begendf | sed 's?^BEGIN$?__@@@__?' | sed 's?^END$?__]]]__?' |\ |
Quote:
And last question just for interesting :) can i use another "delimiter" (or separator or what is correct name) in interval ? /__@@@__/,/__]]]__/ tried ?__@@@__?,?__]]]__? but did not work |
Quote:
Well $? stores the result of the last command you have run. If a command is successful it returns 0 else 1. For .eg. Code:
-sh-3.00$ rm abc Code:
-sh-3.00$ touch abc |
Using this sample input file, modified slightly for the first BEGIN,END block
Code:
$ more file Code:
awk ' Code:
$ ./shell.sh |
Quote:
no its not. Its ugly, unreadable and full of unnecessary chaining. Use sed for simple substitution. |
@ghostdog74: You are not using the original posted input. Other solutions are indeed needed if the input is different, but that wasn't the OP's original question.......
Quote:
I'm talking about this one: sed '/^BEGIN/,/^END/{s/ BEGIN/ START/;s/ END/ EXIT/}' infile |
Quote:
I just meant about these steps, not the code. I could not understand the code even. ;) Yes, this can be done by sed in just three lines as per my knowledge. Code:
1. temporarily replace 'BEGIN' and 'END' that start a line |
Quote:
such as BEGIN and END". there may be other ENDs and BEGINs at different places inside BEGIN,END blocks, besides the ones OP posted Quote:
Also, I did not say its wrong to use small elegant sed solution. I did say use sed for simple substitution. Anything beyond , like that of post #5, code will get ugly and hard to decipher especially if anything went wrong. |
All times are GMT -5. The time now is 02:30 PM. |