help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to grep the last match out from a bunch of files


From: Greg Wooledge
Subject: Re: [Help-bash] how to grep the last match out from a bunch of files
Date: Tue, 27 Dec 2011 13:00:02 -0500
User-agent: Mutt/1.4.2.3i

> On Wednesday 28,December,2011 01:12 AM, Greg Wooledge wrote:
> >awk '/^step$/ {getline; step=$0} END {print step}'

On Wed, Dec 28, 2011 at 01:33:53AM +0800, lina wrote:
> sorry, too concise for me to be able to put into practice about the 
> bunch of files part.

> $ more try.sh
> #!/usr/bin/awk
> 
> awk 'BEGIN{
>     /^step$/
>     }
>     {
>     getline;
>     step=$0
>     }
> END{
>     print step
> }' $0

You do not want to use BEGIN here.  You want the /^step$/ pattern to be
matched against each line of input.

If you want it to be an "awk script", then do this:

#!/usr/bin/awk
/^step$/ { getline; step=$0 }
END      { print step }

Both this, and my original, only operate on *one* file at a time.  If
you want to run it against multiple files, then you either need to
rewrite it, or just run it from a shell loop:

for f in a_*.txt; do
  awk '....' "$f"
done



reply via email to

[Prev in Thread] Current Thread [Next in Thread]