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:
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:
Thanks very much in advance. |
Why not just use one find statement once you are logged in ?
Code:
|
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. |
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. |
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:
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. |
Quote:
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 |
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 |
You might have to convert you patterns variables to this format using sed or something
|
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. |
Probably because you've got a version of grep for AIX,
Read the local man page, but on Linux Code:
grep -V |
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. |