Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex in shell scripting

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello everyone,

wondering if someone could please help me with the following problem. In the code shown below currently I am just running a search for any files that match dc001.anything.log. and then each log file that it finds I printout everything after the forth line. What I am trying to do is instead of printing everything after the fourth line I would like to print everything after a regex match for a string. For example if I have a file like the one below, I would like to match on the last occurrence of 'CONNECTION' then print everything after that match. I am pretty new to shell scripting so I am not even sure if this can be done.


Code:
#!/bin/sh

DIR=$1

cd ${DIR}
find . -name 'dc001\.*\.log' -print | \
while read log
do
  tail +5 ${log} > ${log}.deleting
  mv ${log}.deleting ${log}
done


SAMPLE FILE

CONNECTION
CONNECTION
RESET
CONNECTION
BLAH
BLAH
BLAH
IP ADDRESS
BLAH

Based on the sample file above the script should yield the following results/output.
Code:
BLAH
BLAH
BLAH
IP ADDRESS
BLAH

thanks for the the help in advance.
 
Hi

A nicer but memory wasting solution with [tt]awk[/tt] :
Code:
awk '$0=="CONNECTION"{delete(b);n=0;next}{b[n++]=$0}END{for(i=0;i<n;i++)print b[i]}' /input/file > /output/file
An efficient solution as we learned from PHV :
Code:
awk 'FNR==NR&&$0=="CONNECTION"{n=FNR}FNR!=NR&&FNR>n' /input/file /input/file > /output/file

Feherke.
 
Something along the lines of
Code:
#!/bin/ksh

IFS='
'

while read line
do
  [[ "$line" = CONNECTION ]] && { > /output/file; } || { echo $line >> /output/file; }
done < /input/file

i.e. every time it comes to a line that matches CONNECTION it resets the output file. Not very efficient but another way to do it.


On the internet no one knows you're a dog

Columb Healy
 
I don't know why (;-)) but I like the last feherke's suggestion:
Code:
awk 'FNR==NR&&$0=="CONNECTION"{n=FNR}FNR!=NR&&FNR>n' $log $log > $log.$$ && mv $log.$$ $log

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Given that your name is 'learning perl' try
Code:
perl -e 'my @op;while (<>) {/CONNECTION/ and @op=(), next or push @op, $_;} print @op;' $log > $log.$$ && mv $log.$$ $log

On the internet no one knows you're a dog

Columb Healy
 
columb..

Dude thanks for the help. I completely forgot that I could do this is a perl one liner you are the MAN I was able to put that in my code successfully.

thanks for the help everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top