sed problem...
Hey all. I need a certain line to be printed from a file but am having troubles.
Code:
foo Code:
sed 's/\(.*\)foo/\1foomark/' test |
Sed usually works on one line at a time - replacing the first or last instance of a pattern means the first or last on each line of input.
|
to know that it has found the last "foo", it has to go the end of the file. In addition, it has to remember where the last "foo" was, and then print the next line.
It seems like you will need a loop, with something like awk (gawk) reading one line at a time and with a counter tracking line numbers. When the loop exits, it can return the line number of the last "foo". Then increment this and use it to get the line you want. |
Oh.. I must have misunderstood then. Hmm... I also thought awk would be better able to handle it.
I'll go ahead and post this because it's directly related to my problem. The reason I'm looking to use sed or something similar is because the below code doesn't tranfer my variable. I'm using a logging function to redirect output but I need my variable from blah2 to stick when I call blah1 (which actually checks PIPESTATUS to see if we are done or an error occured). This is a "borrowed" logging facility, thus I don't quite understand how to restructure calling on blah2 so it'll export my variable yet retain it's functionality... Code:
#!/bin/sh |
Hi.
Quote:
Code:
#!/bin/sh Code:
% ./s2 |
here's an awk one, i don't know what you want to do after you found the first bar after foo, so i break it out.
Code:
awk ' /foo/ { c=NR; } |
Thanks guys for the help.. The more I think about it, the more I realize this isn't the way to go. My script is small and compact and I really don't wan't to clutter it up by adding lot's of stuff for this one thing.
Going upstream to the source, my problem lies in the below script by having brackets wrapped around my function call. This whole sed/ed/awk stuff is just to try and come up with $variable.... Which is already defined in 'blah2'. So...... If I can't figure out how to get this to work by modifying my blah2 call, then I'll just throw in the towel I suppose... Bummer. I do appreciate the help tremendously tho... As you can see, if you run this script, $variable doesn't get outputted... I do need to retain my redirects but the .err file is useless and could stand to go away. So.. In short. echo $variable from blah1 while still silencing/redirecting output from blah2... Code:
#!/bin/sh |
Quote:
somehow, you have to "return" the "variable" in blah2 back to the main script. one way to do this is : Code:
.. |
No that's not working.. Good idea tho.. I see what your saying.. What sort of confuses me is, that if you comment my blah2 call with the redirects and then just call it straight out, it works..
|
Code:
#!/bin/sh Code:
blah1() EDIT: Ughhhh.. Something wierd is going on.. My test script works but my actuall build script doesn't... test.sh Code:
#!/bin/sh |
Hi,
# sed -n '/foo/{n;p;}' somefile.txt | tail -1 exp: sed command gives you every first line after every foo and with tail you get the last one which is the one you want. Cheers, |
Quote:
It's too bad I had to start messing around and get obsessed with my above post... ;) I'll tuck that sed away and use it when I'm utterly fed up with trying to figure out why my script doesn't work but the test case does... Thanks guys for your help. It's very appreciated. |
All times are GMT -5. The time now is 06:26 AM. |