Select everything before a pattern

Hi I have i doubt, actually i have to select everything before a word(pattern).For that i am using sed
i am using the below line of code but it is not working i am getting a blank instead..

sed -n '/regexp/{g;1!p;};h' file1

Can anyone help?
Thanks

I don't understand your approach, but I got this to work:

sed -n '/regex/{;s/regex.*//p;q;};p' /some/file

sed -n -e '/regex/q'  -e '1,/regex/p'

Hi ,

got this to work:
sed -n -e '/regex/q' -e '1,/regex/p'

Thanks all for your quick reply..!!

Hi All i have a similar doubt regarding sed.

Actually i have to select some line netween two gegex but my problem is everything except the ending regex should be selected.

i am using this

cat websphere_stdout.txt |sed -n '/0x291e0/,/^"/p'

It is giving me the output like this:

"P=913124:O=0:CT" prio=5 tid=0x291e0 nid=0x1 waiting on monitor [0xffbed000..0xffbeda84]
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:415)
        at com.ibm.ws.runtime.Server.awaitShutdown(Server.java:1710)
        at com.ibm.ejs.sm.server.ManagedServer.main(ManagedServer.java:172)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.ibm.ws.bootstrap.WSLauncher.main(WSLauncher.java:158)

"VM Thread" prio=5 tid=0xef0c0 nid=0x4 runnable

my the required output is this:

"P=913124:O=0:CT" prio=5 tid=0x291e0 nid=0x1 waiting on monitor [0xffbed000..0xffbeda84]
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:415)
        at com.ibm.ws.runtime.Server.awaitShutdown(Server.java:1710)
        at com.ibm.ejs.sm.server.ManagedServer.main(ManagedServer.java:172)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.ibm.ws.bootstrap.WSLauncher.main(WSLauncher.java:158)

The last line containing " should not come.

Can anyone advice.

sed -n '/0x291e0/,/^$/p' websphere_stdout.txt

Hi Devtakh,

Thanks for you reply.

But here i have given a snippet of my file.

There are many lines like the one i pasted above.

and every new thread starts with ["]

So here if i give

sed -n '/0x291e0/,/^$/p' websphere_stdout.txt

it will print everything except the last line but that is not the case.

i have to select every thing before ["]

Thanks

sed -n '/0x291e0/,/^\"/{s/^\"[^P].*//g;p}' file

Hi Devtakh,

Thanks for your reply.
But are you sure this solution works???

because when i have used this code

cat websphere_stdout.txt |sed -n "/`cat /tmp/thrd_id | head -2 | tail -1`/,/^\"/{s/^\"[^P].*//g;p}"

i am getting this:

sed: command garbled: /16aa880/,/^"/{s/^"[^P].*//g;p}

can you help??

Try this with awk:

awk '
/0x291e0/{f=1;print;next}
/"/ && f{exit}
f' file
 
sed -n '/0x291e0/,/^"/p' websphere_stdout.txt | sed '$d'

That is because you haven't used \ on the " character.
Check the error msg you received and compare it with the one that i had given.

Hi Franklin.

In the code above which you have given will it be possible to pass a variable instead of the value that is being passed a a regex??

awk -v var="0x291e0" '
$0 ~ var{f=1;print;next}
/"/ && f{exit}
f' file

Regards

Hey Franklin,
Thanks for your reply...
Can you suggest some bokk for a beginner like me where i can learn more tricks and things that can be done with awk,sed,nawk etc??

You can find some tutorials here:

http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-programming-tutorials-shell-scripting-tutorials.html

One of the books I can recommend is: sed & awk, Second Edition by Dale Dougherty, Arnold Robbins