LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can grep search for multiple patterns from a variable (https://www.linuxquestions.org/questions/linux-newbie-8/can-grep-search-for-multiple-patterns-from-a-variable-4175434702/)

pradeepdee6 10-30-2012 01:14 AM

Can grep search for multiple patterns from a variable
 
Hello Everyone,

Im working on a unix script which gets connected to a remote server through SFTP and gets file mentioned in a pattern.

I have to change the permissions of those files.
Problem is i can not create a temp file to hold patterns. Patterns will be stored in variable.

I have developed a code to capture the files which we get in FTP process.

Quote:

Script:

FILES="pattern1" ### Patterns used to get

FILEDIR="/home/pradeep/newtest"

set -f
GETFILES=`for i in $FILES; do print "get "$i""; done`
set +f


BATCHFILE=/tmp/Testget.CON.PRO.$TIMESTAMP.$$.tmp
echo "cd /$FILEDIR/Inbox
$GETFILES
bye" > $BATCHFILE ### creating temp BATCHFILE to use in SFTP process



## FTP PART ###

MFTCMD="sftp $SSHOPTIONS -o IdentityFile=$HDIR/.ssh/$PRIVATEKEY -b $BATCHFILE $SFTPUSER@$MFTHOST"

$MFTCMD


MYFILES=`find . -newer $BATCHFILE | grep "$FILES" `


if [ "$PERMISSIONS" != "" ]; then
chmod $PERMISSIONS $MYFILES
if [ $? -eq 0 ] ; then
echo " SSH set permissions of $MYFILES to $PERMISSIONS"
else
echo "SSH command \"chmod $PERMISSIONS $MYFILES\" failed with exit code $?"
fi
fi

This works fine when there is only one pattern.

The grep can not work search multiple patterns unless patterns are fed through a file.
I tried using a while loop. but not able to find a solution.

Quote:

echo $FILES | while read line
do

find . -newer $BATCHFILE | grep "$line"

done
Please help me how to use grep to read multiple patterns from a variable.

Thanks very much in advance.

jv2112 10-30-2012 04:35 AM

Why not just use one find statement once you are logged in ?

Code:


find "Path" -type f -and -iname "Pattern" -and ! -perm "Octal Wanted" -exec chmod "octal" {} \;

This should work on all assuming you find the conditions. :twocents:

pradeepdee6 10-30-2012 10:14 AM

Thanks so much jv2112.

I had tried the find command. It will list all the files matching the pattern(even those created much before).
i need to capture only the files which are created in SFTP GET process.

If i was using a file to store the patterns then the above works efficiently. Since i will have to fetch the patterns through a variable im not able to use it.

David the H. 10-30-2012 01:03 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


And the important environmental criterion is which shell you're using. Does this have to be a POSIX compatible sh script, or can you use bash or ksh? Or what? With either of the latter you could probably use arrays to simplify things.


Some other general suggestions:

1) $(..) is highly recommended over `..`

2) Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.

3) Variables are for data, not code, so don't store commands in them. Use a function instead. Or just run it directly if you're only going to do it once.

pradeepdee6 11-05-2012 04:10 AM

Hi Thanks very much for ur responses.

Im Using ksh shell.
I have come to an solution for my above problem.

Im using for loop to extract the variables and pass it on to while loop to my function.

Quote:

Code:

for i in 1 2 3 4 5
do
echo "$var" | cut -f$i -d " "
done | while read line
do
find . -newer $BATCHFILE | grep "$line"
done

I tried to Use a variable to hold the count or patterns in variable.
But variable usage in for loop is not supported in ksh and bash shells.

please help if the above process can be optimised.

Thanks in Advance.

pixellany 11-05-2012 04:23 AM

Quote:

But variable usage in for loop is not supported in ksh and bash shells.
I don't think that's correct...

Your code seems awkward---normally the "while read" construct is used directly on a file---and not on piped output from a prior command. Why not just operate directly on the data generated in the for loop?

Finally, it looks like you're using both CODE and QUOTE tags--all you need is CODE----but also format you code for readability---e.g. like this:

Code:

for i in 1 2 3 4 5; do
    echo "$var" | cut -f$i -d " "
done |
while read line; do
    find . -newer $BATCHFILE | grep "$line"
done


ba_kirang 11-05-2012 06:12 AM

AFAIK grep can be used for multiple patterns:

====================
grep "pattern1\|pattern2\|pattern3" file
====================

I am not sure this will work if these patterns are regexps

ba_kirang 11-05-2012 06:13 AM

You might have to convert you patterns variables to this format using sed or something

pradeepdee6 11-08-2012 01:45 AM

Thanks very much ba_kirang,

But unfortunately im not able to grep multiple patterns as you suggested.
Im able to match multiple patterns using the below format

egrep "(pattern1)|(pattern2)|(pattern3)" file

Im using bash and ksh shell and version: AIX appax023 1 6 0001AC89D600

Why im not able to use grep "pattern1\|pattern2\|pattern3" file

Thanks in Advance.

chrism01 11-08-2012 04:18 AM

Probably because you've got a version of grep for AIX,
Read the local man page, but on Linux
Code:

grep -V
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

It may not support multiple patterns, or it may use a different syntax.

pradeepdee6 11-08-2012 04:52 AM

Thanks chrism01,

Then i will work with egrep as i can achieve multiple pattern matching in egrep.

Thanks very much.


All times are GMT -5. The time now is 11:10 PM.