Re: search a file for a string and return the next line ONLY



MyUsenetAccount wrote:
Ed Morton wrote:

thomasriise wrote:


try this:

awk '/pattern/ {getline ; print}' filename

- that's how you get the line you want to replace. The rest you should
be able to fix yourself.


getline is loaded with traps and caveats, so it's best to avoid it. This
would do the same:

awk 'f{print;f=0}/pattern/{f=1}' filename

Ed.

I tried the 2nd suggestion above like so:

StringToReplace=`awk 'f{print;f=0}/${TAG}/{f=1}' "$File"`

but StringToReplace then does not contain anything? Is the syntax
correct?

That depends what "TAG" is, but I'll go out on a limb and assume it's a shell variable that you're trying to pass to awk, in which case the answer is no. The correct syntax for that would be:

StringToReplace=`awk -v tag="$TAG" 'f{print;f=0}$0 ~ tag{f=1}' "$File"`

See question 24 in the FAQ, http://home.comcast.net/~j.p.h/cus-faq-2.html#24, for details.

Unfortuantely I know very little about awk and so would
welcome some more help to get this command to work.
Also back to my orginal question:
Can I use (f)grep or do I need to resort to awk?

Thanks to an earlier poster, there isn't enough context left in this posting to tell what you were trying to do and I'm too lazy to go looking for the original so I can't answer.

Ed.
.