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.
.



Relevant Pages

  • Re: How to filter data file with double-quotes
    ... I couldn't find a fix for Cesar's version... ... Ed's solution 'prints out negative values as positive' because his analysis of the problem led him to create a script where the character "-" is understood by AWK as a separator. ... Again, in Ed's solution becouse he cleverly instructed AWK to consider '"' as a separator chararacter, you may end up with blanks or not the numberyou expect for the last two values. ...
    (comp.lang.awk)
  • Re: Seeking help with Sed statement.
    ... To my understanding of sed it would be: ... I've been able to write the fix in awk, ... I just reread your post since Chris pointed out you want only the ...
    (comp.unix.programmer)
  • Re: Seeking help with Sed statement.
    ... To my understanding of sed it would be: ... I've been able to write the fix in awk, ... I just reread your post since Chris pointed out you want only the ...
    (comp.unix.shell)
  • Re: CONFIG_KPROBES=y build requires gawk
    ... Your awk doesn't support charactor-class. ... Please try to use gawk. ... Aside of the incorrect suggestion (you need not just gawk, ... to fix, the same fix will remove any benefits compared to universally ...
    (Linux-Kernel)
  • Re: Sorting logfile entries
    ... Any hints? ... Are you limited to stream oriented tools (sed, awk, etc)? ... Don't compensate for errors by covering them up - fix them and move on. ...
    (comp.programming)