LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-30-2012, 02:14 AM   #1
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Rep: Reputation: Disabled
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.
 
Old 10-30-2012, 05:35 AM   #2
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
Lightbulb

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.
 
1 members found this post helpful.
Old 10-30-2012, 11:14 AM   #3
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-30-2012, 02:03 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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.
 
1 members found this post helpful.
Old 11-05-2012, 05:10 AM   #5
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-05-2012, 05:23 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 11-05-2012, 07:12 AM   #7
ba_kirang
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 11-05-2012, 07:13 AM   #8
ba_kirang
LQ Newbie
 
Registered: Apr 2012
Posts: 17

Rep: Reputation: Disabled
You might have to convert you patterns variables to this format using sed or something
 
1 members found this post helpful.
Old 11-08-2012, 02:45 AM   #9
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-08-2012, 05:18 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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.
 
1 members found this post helpful.
Old 11-08-2012, 05:52 AM   #11
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
Smile

Thanks chrism01,

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

Thanks very much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching for multiple patterns with grep dznunn Programming 2 06-26-2012 10:52 AM
[SOLVED] grep many files in multiple directories using patterns from a file francy_casa Linux - Newbie 4 04-12-2012 09:49 AM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 08:41 PM
Excluding multiple patterns from grep flamingo_l Linux - General 9 01-06-2011 09:05 AM
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 07:07 PM


All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration