Monday 24 December 2012

Fatch line upto eof

fatch_line_upto_eof

1.
awk -f t.awk xyz.log

::::::::::::::
t.awk
::::::::::::::
BEGIN { lines=0; }
{ if ( lines > 1 ) line[lines++] = $0; }
/timerFactory/ { lines = 1 ;line[lines++] = $0; }
END { for (i=1;i<lines;i++) print line[i];}
::::::::::::::


2. ### To confirm that I understand your goal:
### You want to scan a file, looking for string "timerFactory"...
### ( which may occur multiple times in the file )
### When that string is found, for the LAST time in that file,
### you want to output that line, and all lines to end-of-file.

### I think "sed" can do that for you, like this:

sed -n 'H;${g;p;}
/timerFactory/h' < your_log_file.txt

orsed -n 'H;${g;p;} ; /timerFactory/h' < your_log_file.txt


Yes, the extra line-break is needed for many flavors of unix+sed.
( for others, a semi-colon will let you keep it with one line )

Anybody ELSE have a faster/shorter "one-pass" solution ?

### Re-reading, This actually has logic similarities to Jan's solution above.
### How does it work? There is also a 'special case' miss... (can you find it?)

hint:
sed -n '1,/timerfactory/d;H;${g;p;}
/timerFactory/h' < your_log_file.txt

### For a two-pass:
GET_LINE=$(awk '/timerFactory/{lc=NR}END{print lc?NR-lc+1:0}' < log_file)
tail -$GET_LINE} source_file

### Another one-pass:
awk '/timerFactory/{x=1}lc{buf[x++]=$0}
END{d=x;while(d--){print buf[x-d]}}' < source_file


### If using pure "shell" you could try this:
###
LC=0;BUFF=
while read LINE ; do
case "$LINE" in (*timerFactory*) BUFF="${LINE}" ;LC=1 ;;
*) ((${LC})) && BUFF="${BUFF}
${LINE}" ;;
esac
done < source_file
[ "${#BUFF}" -gt 0 ]&& echo "${BUFF}" ;unset BUFF

### make note of extra [cr] or [nl] inside quotes.
### keeping multiple lines in the same ENV variable

### You could even decide to use a file, as buffer rather than Memory.
### (although a bit slower, so I wouldn't reccomend)

while read LINE ; do
case "$LINE" in (*timerFactory*) LC=1 ; : > Tem.$$ ;; #clear file
esac ; echo "${LINE}" >> Tem.$$
done < source_file
((${LC})) && cat Tem.$$
[ -f Tem.$$ ] && rm Tem.$$

### maybe even this:

tac source_file | sed '/timerFactory/q' | tac

##although "tac" is not available on all UNIX platforms (mostly Linux)

AND, if you really only wanted the sum(Total) since last "timerFactory" line,
you could also 'awk' the whole thing, and print Summary, like this:

awk '/timerFactory/{day=$1;fc=tot=0;mk=NR}
$2~"^Total"{fc++;split($2,val,"=");tot+=val[2]}
END && day {print "Total:"tot,"since",day,"files:"fc,"line:"mk} ' source_file


3. tail -$(grep -n "timerFactory" log.log | tail -1 | awk -F: '{ print $1}') log.log 


No comments:

Post a Comment