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: lina
Subject: Re: [Help-bash] how to grep the last match out from a bunch of files
Date: Wed, 28 Dec 2011 14:04:11 +0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111114 Icedove/3.1.16

On Wednesday 28,December,2011 02:00 AM, Greg Wooledge wrote:
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
Thanks, I am not familiar with awk so well.

$ for f in a_*.txt; do echo -n -e "$f\t" ; grep -A 1 step $f | tail -1 ; done
a_1.txt    3
a_2.txt    4

$ for f in a_*.txt ; do echo -n -e "$f\t" ; awk '{/^step&/ getline; print $0}' $f | tail -1; done
a_1.txt    3
a_2.txt    4

Come up above 2 with your hints.

Best regards,






reply via email to

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