[Top][All Lists]
[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 01:33:53 +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 01:12 AM, Greg Wooledge wrote:
On Wed, Dec 28, 2011 at 01:03:33AM +0800, lina wrote:
I have a bunch of files like a_*.txt
it contains many "step" in a_*.txt
$ cat a_1.txt
step
1
step
2
step
3
some lines appended here
I wish to get the final step,
for a_1.txt it's 3
awk '/^step$/ {getline; step=$0} END {print step}'
Thanks for you quick reply,
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
$ ./try.sh a_*.txt
awk: ./try.sh
awk: ^ syntax error
awk: ./try.sh
awk: ^ unterminated regexp
Thanks ahead with best regards,
It could be made more efficient, e.g. by skipping all the rest of the
reads when you don't match ^step$, but that will suffice for a teaching
example.
Awk reads input line by line, and applies checks/actions to each line.
I told it to act when it sees a line that matches the regular expression
pattern ^step$. When it matches, it will read the next line from the
input ("getline"), and then set the "step" variable to the content of that
line. It keeps doing that until there are no more lines.
Then at the END of the input, it prints the contents of the step variable.