[Novalug] Unix script help request
James Ewing Cottrell 3rd
JECottrell3 at Comcast.NET
Fri Feb 27 13:04:38 EST 2009
Well, it has been said that any program that works is a good one, but it
can be done more eloquently:
grep -l pattern files |
while read name
do
mv $name S$name
done
Or, if you are into using xargs:
grep -l pattern files | xargs -i @ mv @ S@
I hate to be a wet blanket, but there are several things wrong with
Garrett's script:
[1] grep is executed once per file rather than once only.
[2] grep -l will quit reading when a file once a pattern. His version
unnecessarily reads the rest of the file after the first match.
[3] anytime you test $? you missed an opportunity to use if:
if grep ....
then it_matched
else it_did_not
fi
[4] this is more of a pet peeve of mine, but I would rather use a case
statement to do string matching:
case $? in
(0) success;;
(*) failure;;
esac
The reason is that the == (or is it = ?) and -eq are the *opposite* from
what perl uses, and in my mind, Perl's version make the most sense.
One way to debug this is to place an echo in front of the mv to see what
kind of commands will be generated. Then, when you are done, either take
the echo out, or pipe it to sh.
And yes, you can pipe both into and out of a while statement:
grep -l pattern files | while read x; do echo mv $x S$x; done | sh -x
grep -l pattern files | xargs -i @ echo mv @ S@ | sh -x
JIM
Garrett Nievin wrote:
> One possible solution:
>
> If you do a grep with the "-q" option (quiet), it will just set the
> exit code to 0 if the text was found and 1 if it was not. You can
> check the exit code using the shell variable ?. The question mark
> character is the name of the variable.
>
> Here's some nice structured mainframe-y code:
>
> # see if $loopfile contains the magic string, and do something accordingly
> grep -q '<TRANS_CDE>RSCF' $loopfile
> if [ $? -eq 0 ] ; then
> echo "string was found"
> else
> echo "string was not found"
> done
>
>
>
> On Sat, 21 Feb 2009 08:32:38 -0500
> Kevin Starkey <kevin.linuxfan at gmail.com> wrote:
>
>> Hi all,
>>
>> My Linux experience (meager as it is) is finally starting to be
>> applicable at my job (mainframe programmer). We just started using a
>> Unix file server on the mainframe and occasionally we run into an issue
>> to solve and I volunteered to tackle this one. The issue is, I might
>> have 0, 1, or > 1 files in a directory with the same name pattern of
>> "EBPV************.out.xml" and one of these files might be the one I'm
>> looking for, or it might not. I need to identify a particular string
>> inside of the file, and if it matches, then I want to add an 'S' to the
>> start of the name (SEBPV....).
>>
>> Below is the start of a script that I hope is close to what I need, but
>> I need to add "grep" and a conditional statement where I only do the
>> renaming if the grep finds what I'm looking for, but I'm not sure how to
>> code that part (this is my first script).
>>
>> ---------------------------------------------------------------------------------------------------
>> #!/bin/bash
>>
>> # Change to directory
>> cd /proj/preed/output
>>
>> #Get all needed files in current directory
>> originalFiles=$(ls EBPV*.out.xml)
>>
>> # Loop through all files and do your changes
>> for loopFile in $originalFiles
>> do
>> # Create your new filename including the extension
>> mv $loopFile S$loopFile
>> done
>> ---------------------------------------------------------------------------------------------------
>>
>> I need to somehow add:
>>
>> grep $loopfile "<TRANS_CDE>RSCF"
>>
>> and only if it's found then I want to do the "mv ...." (renaming).
>>
>> Any help would be greatly appreciated.
>>
>> Thanks,
>> Kevin.
>>
>> _______________________________________________
>> Novalug mailing list
>> Novalug at calypso.tux.org
>> http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
>>
>
>
>
> _______________________________________________
> Novalug mailing list
> Novalug at calypso.tux.org
> http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
>
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.0.237 / Virus Database: 270.11.2/1963 - Release Date: 02/20/09 19:22:00
>
More information about the Novalug
mailing list