SUMMARY: korn shell script question

From: Brehl, Blake (Blake.Brehl@anritsu.com)
Date: Thu Oct 23 2003 - 19:39:48 EDT


Hi,

Thank you for the many replies received, I ended up inserting a "| tail -1"
before the awk to do the trick. I didn't reflect the output correctly in the
original post, as 3 5 7. It should have been:
3
5
7

Here's the reply's:

grep -in "End of Report" somefile | tail -1 | awk -F: '{print $1}'
grep -in "stuff" somefile | awk -F: '{print $1}' | tail -1
grep -in "End of Report" somefile | awk '{s=$1};END{print s}'
grep -in "End of Report" somefile | awk -F: '{print $1}' | tail -n 1
grep -in "End of Report" somefile | awk -F: '{last = $1} END {print last}'
#to avoid spawning more processes in the pipe
grep -in "End of Report" somefile | awk -F: '{print $1}'|tail -1
grep -in "End of Report" somefile | cut -f1 -d: | tail -n1

And an interesting one from Phil Farrell:

This is not really a Korn shell question, but rather, what UNIX utility to
use for your purpose. How about doing the whole thing in awk, for example:

        awk '/End of Report/ {found=NR};END{print found}' somefile

Each time it finds the line with the regular expression within slashes, this
little program updates its "found" variable with the line number. Then,
after looking at all input lines, in the END statement it prints the value
of the "found" variable, which will be the line number of the last line
found with that expression.

Many thanks to Iain, Jay, Mandell, Kris, James, Charles, Marcelo, Phil and
Dr. Thomas.

Best Regards, Blake

Thank you

-----Original Message-----
I want to identify the number of the line of the last occurance of the
string "End of Report" within a file.

#> more somefile
line1
line2
End of Report
line4
End of Report
line6
End of Report
#>

#> #!/bin/ksh
#> grep -in "End of Report" somefile | awk -F: '{print $1}'

results in

3 5 7

Is there a way to capture the last value (7), when you don't know how many
occurance? I have gnugrep loaded if that is a better command.
  
TRU64UNIX V5.1A sp4

Best Regards,

Blake Brehl
Sys Admin/DBA
Anritsu Company United States
Morgan Hill, CA



This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:49:39 EDT