[SOLVED] Can grep search for multiple patterns from a variable
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.