|
Home > Archive > Unix Shell > May 2004 > reg exp in groups of lines?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
reg exp in groups of lines?
|
|
|
| Hello unix shell group members,
I've written a script in which I need to grep a few consecutive lines
from the Autosys autorep command and then determine if one line is
valid or not and if not, identify the job which is on the previous
line. Here's the code - it gives me the correct lines but how do I
validate one and if it fails, pull the previous line to identify it:
autorep -L 0 -J %tara% -q | awk '/command/ {print old2 ; print old1 ;
lines2print=3 }
{ if (lines2print>0) { print $0 ; lines2print-- ; }
old2=old1 ; old1=$0 ; } ' |
while read a b c ; do
if [[ $b = "\$\$"* ]]
then
if [[ $b != "\$\$dev8cmd\_dir/"* ]] && [[ $b !=
"\$\$gfcmd_dir/"* ]] && [[ $b != "\$\$vtstcmd_dir/"* ]]
then
job=`echo $old2`
echo "$b is an invalid cmd line in $job " >> auto.out
fi
elif [[ $b != "/autosys/dev8/script/gf" && $b !=
"/autosys/prod/script/gf" || $b != "/autosys/vtst/script/gf" ]]
then
echo "$b is invalid" >> auto.out
fi
So far the output is like this:
is invalid
tara_test3 is invalid
autosys/prod/script/gf/tara_test3 is invalid
chbsp203e is invalid
gfsbatch@chbsp203e is invalid
these are not all invalid.
Thanks,
Tara
| |
| Ed Morton 2004-05-13, 12:52 pm |
|
Pea wrote:
> Hello unix shell group members,
> I've written a script in which I need to grep a few consecutive lines
> from the Autosys autorep command and then determine if one line is
> valid or not and if not, identify the job which is on the previous
> line. Here's the code - it gives me the correct lines but how do I
> validate one and if it fails, pull the previous line to identify it:
>
> autorep -L 0 -J %tara% -q | awk '/command/ {print old2 ; print old1 ;
> lines2print=3 }
> { if (lines2print>0) { print $0 ; lines2print-- ; }
> old2=old1 ; old1=$0 ; } ' |
> while read a b c ; do
> if [[ $b = "\$\$"* ]]
> then
> if [[ $b != "\$\$dev8cmd\_dir/"* ]] && [[ $b !=
> "\$\$gfcmd_dir/"* ]] && [[ $b != "\$\$vtstcmd_dir/"* ]]
> then
> job=`echo $old2`
> echo "$b is an invalid cmd line in $job " >> auto.out
> fi
> elif [[ $b != "/autosys/dev8/script/gf" && $b !=
> "/autosys/prod/script/gf" || $b != "/autosys/vtst/script/gf" ]]
> then
> echo "$b is invalid" >> auto.out
> fi
I usually find case statements a lot easier to read:
case $b in
"\$\$"* )
case $b in
"\$\$dev8cmd\_dir/"* ) ;;
"\$\$gfcmd_dir/"* ) ;;
"\$\$vtstcmd_dir/"* ) ;;
* ) job=`echo $old2`
echo "$b is an invalid cmd line in $job " >> auto.out
;;
esac
;;
* )
case $b in
"/autosys/dev8/script/gf" ) ;;
"/autosys/prod/script/gf" ) ;;
"/autosys/vtst/script/gf" ) ;;
* ) echo "$b is invalid" >> auto.out ;;
esac
;;
esac
Try that. If it doesn't work, it should be a lot easier for you to debug
than your nested negated ifs. If you still can't figure it out, post
which of your inputs you expected to be valid since they do all look
invalid to me according to the REs you wrote (I suspect you missed the
"*" off the end of your second batch of REs which, if it were present,
would make "autosys/prod/script/gf/tara_test3" valid).
Ed.
| |
| Ed Morton 2004-05-13, 12:52 pm |
|
Ed Morton wrote:
>
>
> Pea wrote:
>
>
>
> I usually find case statements a lot easier to read:
>
<snip>
It'd also be even more readable if we switch the case nesting (or you
could go back to an "if" just for the error-leg case):
case $b in
"\$\$dev8cmd\_dir/"* ) ;;
"\$\$gfcmd_dir/"* ) ;;
"\$\$vtstcmd_dir/"* ) ;;
"/autosys/dev8/script/gf" ) ;;
"/autosys/prod/script/gf" ) ;;
"/autosys/vtst/script/gf" ) ;;
* ) case $b in
"\$\$"* )
job=`echo $old2`
echo "$b is an invalid cmd line in $job " >> auto.out
;;
* ) echo "$b is invalid" >> auto.out ;;
esac
;;
esac
Regards,
Ed.
| |
| Ed Morton 2004-05-14, 12:49 pm |
|
Ed Morton wrote:
>
>
> Ed Morton wrote:
>
> <snip>
>
> It'd also be even more readable if we switch the case nesting (or you
> could go back to an "if" just for the error-leg case):
<snip>
Hmm, re-reading this, it occurs to me that you don't actually need to
nest the case statments:
case $b in
"\$\$dev8cmd\_dir/"* ) ;;
"\$\$gfcmd_dir/"* ) ;;
"\$\$vtstcmd_dir/"* ) ;;
"/autosys/dev8/script/gf" ) ;;
"/autosys/prod/script/gf" ) ;;
"/autosys/vtst/script/gf" ) ;;
"\$\$"* ) job=`echo $old2`
echo "$b is an invalid cmd line in $job " >> auto.out
;;
* ) echo "$b is invalid" >> auto.out ;;
esac
Glad it took me 3 postings to get there 8-(.
Ed.
| |
|
| Ed Morton <morton@lsupcaemnt.com> wrote in message news:<wKadnZ4elJPRMzndRVn-uw@comcast.com>...
> Ed Morton wrote:
> <snip>
>
> Hmm, re-reading this, it occurs to me that you don't actually need to
> nest the case statments:
>
> case $b in
> "\$\$dev8cmd\_dir/"* ) ;;
> "\$\$gfcmd_dir/"* ) ;;
> "\$\$vtstcmd_dir/"* ) ;;
> "/autosys/dev8/script/gf" ) ;;
> "/autosys/prod/script/gf" ) ;;
> "/autosys/vtst/script/gf" ) ;;
> "\$\$"* ) job=`echo $old2`
> echo "$b is an invalid cmd line in $job " >> auto.out
> ;;
> * ) echo "$b is invalid" >> auto.out ;;
> esac
>
> Glad it took me 3 postings to get there 8-(.
>
> Ed.
Thanks Ed. I appreciate it. Now that I have those IF statments
looking neater, how can I read the 3 lines I'm grepping as 1? Meaning
that each time the awk statement finds a match, how can I read through
only the 3 selected lines instead of a whole output file (which would
be huge) ?
| |
| Ed Morton 2004-05-17, 3:35 pm |
|
Pea wrote:
<snip>[vbcol=seagreen]
> Thanks Ed. I appreciate it. Now that I have those IF statments
> looking neater, how can I read the 3 lines I'm grepping as 1? Meaning
> that each time the awk statement finds a match, how can I read through
> only the 3 selected lines instead of a whole output file (which would
> be huge) ?
I'm not 100% sure what you're asking, but if it's what I think then just
add an "exit" to the awk commands, e.g.:
awk '/pattern/ { cnt = 1 }
cnt > 0 { print; cnt++ }
cnt == 4 { exit }'
would print whatever line contains "pattern" plus the subsequent 2 then
not read any more of the input file. Alternatively, take a look at the
-B and -A options for GNU grep.
Ed.
|
|
|
|
|