grep and shell substituion
am 31.01.2008 00:02:39 von bdorsey63
I am attempting to grep for a file mask $i. it could be anything, *, *.
57*, dup*, *stuff*
I cannot get this to work right, becuase my grep doesnot return what I
expect. I want it to lork like ls works, where the file mask returns
those lines. But grep doesn;t work like i expet it.
Any help would be great.
tempdir=any directory
sname=name of script
filelist= a file listing from a ls -al
example:
-rwxr-xr-x 2 root root 2003 Jun 13 2007 zdiff
-rwxr-xr-x 3 root root 3156 Jun 13 2007 zegrep
-rwxr-xr-x 3 root root 3156 Jun 13 2007 zfgrep
-rwxr-xr-x 1 root root 1016 Jun 13 2007 zforce
-rwxr-xr-x 3 root root 3156 Jun 13 2007 zgrep
-rwxr-xr-x 1 root root 68812 Nov 19 2004 zip
-rwxr-xr-x 1 root root 28172 Nov 19 2004 zipcloak
-rwxr-xr-x 1 root root 1188 Mar 26 2007 zipgrep
-rwxr-xr-x 2 root root 110096 Mar 26 2007 zipinfo
-rwxr-xr-x 1 root root 24080 Nov 19 2004 zipnote
-rwxr-xr-x 1 root root 27284 Nov 19 2004 zipsplit
-rwxr-xr-x 1 root root 41 Jun 13 2007 zless
-rwxr-xr-x 1 root root 1878 Jun 13 2007 zmore
-rwxr-xr-x 1 root root 3431 Jun 13 2007 znew
Here is the code:
for i in $filelist
do
if [ $i = "*" ]
then
if (($(cat $tempdir/$sname.tmp | wc -l) == 0))
then
send_msg -o "Error: Remote files look up of $i
returns nothing"
RC=106
else
send_msg -o "Remote files found: `cat $tempdir/
$sname.tmp`"
fi
else
if (($(cat $tempdir/$sname.tmp | grep -E "$i" |
grep -v dr | wc -l) == 0))
then
send_msg -o "Error: Remote files look up of $i
returns nothing"
RC=106
else
cat $tempdir/$sname.tmp | grep "$i" >> $tempdir/
$sname.tmp2
send_msg -o "Remote files found: `cat $tempdir/
$sname.tmp2`"
fi
fi
done
Re: grep and shell substituion
am 31.01.2008 03:21:19 von bdorsey63
On Jan 30, 5:02=A0pm, bdorse...@gmail.com wrote:
> I am attempting to grep for a file mask $i. it could be anything, *, *.
> 57*, dup*, *stuff*
>
> I cannot get this to work right, becuase my grep doesnot return what I
> expect. I want it to work like ls works, where the file mask returns
> those lines. But grep doesn;t work.
>
> Any help would be great.
>
> tempdir=3Dany directory
> sname=3Dname of script
> filelist=3D a file listing from a ls -al
>
> example:
> -rwxr-xr-x =A0 2 root root =A0 =A0 =A0 2003 Jun 13 =A02007 zdiff
> -rwxr-xr-x =A0 3 root root =A0 =A0 =A0 3156 Jun 13 =A02007 zegrep
> -rwxr-xr-x =A0 3 root root =A0 =A0 =A0 3156 Jun 13 =A02007 zfgrep
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A0 1016 Jun 13 =A02007 zforce
> -rwxr-xr-x =A0 3 root root =A0 =A0 =A0 3156 Jun 13 =A02007 zgrep
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A068812 Nov 19 =A02004 zip
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A028172 Nov 19 =A02004 zipcloak
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A0 1188 Mar 26 =A02007 zipgrep
> -rwxr-xr-x =A0 2 root root =A0 =A0 110096 Mar 26 =A02007 zipinfo
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A024080 Nov 19 =A02004 zipnote
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A027284 Nov 19 =A02004 zipsplit
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A0 =A0 41 Jun 13 =A02007 zless
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A0 1878 Jun 13 =A02007 zmore
> -rwxr-xr-x =A0 1 root root =A0 =A0 =A0 3431 Jun 13 =A02007 znew
>
> Here is the code:
>
> =A0 =A0 =A0 for i in $filelist
> =A0 =A0 =A0 do
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if [ $i =3D "*" ]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 then
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (($(cat $tempdir/$sname.tmp | wc -l=
) == 0))
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 then
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 send_msg -o "Error: Remote files l=
ook up of $i returns nothing"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 RC=3D106
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 send_msg -o "Remote files found: `=
cat $tempdir/$sname.tmp`"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fi
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if =A0(($(cat $tempdir/$sname.tmp | gr=
ep -E "$i" | grep -v dr | wc -l) == 0))
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 then
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 send_msg -o "Error: Remote files l=
ook up of $i returns nothing"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 RC=3D106
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cat $tempdir/$sname.tmp | grep "$i=
" =A0>> $tempdir/$sname.tmp2
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 send_msg -o "Remote files found: `=
cat $tempdir/$sname.tmp2`"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fi
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fi
> =A0 =A0 =A0 done
send_msg is a function which sends to standard out.
Re: grep and shell substituion
am 31.01.2008 07:57:36 von Barry Margolin
In article
,
bdorsey63@gmail.com wrote:
> I am attempting to grep for a file mask $i. it could be anything, *, *.
> 57*, dup*, *stuff*
>
> I cannot get this to work right, becuase my grep doesnot return what I
> expect. I want it to lork like ls works, where the file mask returns
> those lines. But grep doesn;t work like i expet it.
grep uses regular expressions (that's what "re" in the name stands for),
not shell wildcards (you seem to have made up the term "file mask" for
this).
>
> Any help would be great.
>
>
> tempdir=any directory
> sname=name of script
> filelist= a file listing from a ls -al
>
> example:
> -rwxr-xr-x 2 root root 2003 Jun 13 2007 zdiff
> -rwxr-xr-x 3 root root 3156 Jun 13 2007 zegrep
> -rwxr-xr-x 3 root root 3156 Jun 13 2007 zfgrep
> -rwxr-xr-x 1 root root 1016 Jun 13 2007 zforce
> -rwxr-xr-x 3 root root 3156 Jun 13 2007 zgrep
> -rwxr-xr-x 1 root root 68812 Nov 19 2004 zip
> -rwxr-xr-x 1 root root 28172 Nov 19 2004 zipcloak
> -rwxr-xr-x 1 root root 1188 Mar 26 2007 zipgrep
> -rwxr-xr-x 2 root root 110096 Mar 26 2007 zipinfo
> -rwxr-xr-x 1 root root 24080 Nov 19 2004 zipnote
> -rwxr-xr-x 1 root root 27284 Nov 19 2004 zipsplit
> -rwxr-xr-x 1 root root 41 Jun 13 2007 zless
> -rwxr-xr-x 1 root root 1878 Jun 13 2007 zmore
> -rwxr-xr-x 1 root root 3431 Jun 13 2007 znew
>
>
> Here is the code:
>
> for i in $filelist
> do
> if [ $i = "*" ]
If $i is *, the shell will expand the wildcard. You should quote it:
if [ "$i" = "*" ]
> then
> if (($(cat $tempdir/$sname.tmp | wc -l) == 0))
UUOC: $(wc -l < $tempdir/$sname.tmp)
> then
> send_msg -o "Error: Remote files look up of $i
> returns nothing"
> RC=106
> else
> send_msg -o "Remote files found: `cat $tempdir/
> $sname.tmp`"
> fi
> else
> if (($(cat $tempdir/$sname.tmp | grep -E "$i" |
> grep -v dr | wc -l) == 0))
Another UUOC, I leave the rewrite as an exercise for you. Also, if you
want to count the number of lines resulting from a grep, you can use its
-c option rather than piping to wc. And if you just want to know if
there are any matching lines, you can just check grep's exit status.
> then
> send_msg -o "Error: Remote files look up of $i
> returns nothing"
> RC=106
> else
> cat $tempdir/$sname.tmp | grep "$i" >> $tempdir/
> $sname.tmp2
Another UUOC.
> send_msg -o "Remote files found: `cat $tempdir/
> $sname.tmp2`"
> fi
> fi
> done
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***