ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
i am trying to write scripts to pass 2 different variables to find command.
find . -name $var1 -exec grep -H $var2
for example: find . -name CDR_2010-07-21 -exec grep -H 9892614477 {} \;
1st variable CDR_2010-07-21(passed by var1) is the file name inside which i am trying to search string 9892614477(passed by var2) .
i have tried following script
" for file in CDR_2010-07-20* CDR_2010-07-12* CDR_2010-08-05* CDR_2010-08-14* CDR_2010-08-11* ;
do
for file2 in 9990035233 9718290475 9899867969 9820048711 9830589910 9891446365;
do find /data6/ -name $file -exec grep -H $file2 {} \; ;
done;
done; "
but the problem is that it searches the each string in all the CDR_ files.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Why are you using this: for file in CDR_2010-07-20* CDR_2010-07-12*......
Bash will expand CDR_2010-07-20* to all files that match. Example: if CDR_2010-07-20_1 and CDR_2010-07-20_2 exist, these 2 will be substituted for CDR_2010-07-20*.
The below code will search for each number in file2 in every CDR file mentioned:
Code:
for file in CDR_2010-07-20 CDR_2010-07-12 CDR_2010-08-05 CDR_2010-08-14 CDR_2010-08-11
do
for file2 in 9990035233 9718290475 9899867969 9820048711 9830589910 9891446365
do
find /data6/ -name $file -exec grep -H $file2 {} \;
done
done
Your example data is not consistent: There are 5 files and 6 search terms.
This should work if both are equal (I added a dummy file):
Code:
#!/bin/bash
# put data in 2 arrays
files=(CDR_2010-07-20 CDR_2010-07-12 CDR_2010-08-05 CDR_2010-08-14 CDR_2010-08-11 CDR_2000-01-01)
sterms=(9990035233 9718290475 9899867969 9820048711 9830589910 9891446365)
# show content arrays (can be removed, just here to show content)
echo ${files[@]}
echo ${sterms[@]}
# loop over all elements of arrays
for (( x=0 ; x<=${#files[@]} ; x++ ))
do
# use element number for specific content
find /data6 -name "${files[$x]}" -exec grep -H "${sterms[$x]}" {} \;
done
you already have the solution for your problem. The for loops will search, as you already observed, for every grep pattern in every file.
To match all files that start with CDR_xxx you can, e. g.
but i found "for loop" script (posted by druna) usefull & real because i have to pass 300 to 400 such arguments.
one thing which i didn't understand in your script was "x<=${#files[@]}" in for statement. would appreciate if you elaborate this.
i was trying it in another way
by putting all CDR_ files name inside textfile a.
"[root@test /tmp]cat a
CDR_2010-07-20
CDR_2010-07-12
CDR_2010-08-05
CDR_2010-08-14
CDR_2010-08-11
[root@test /tmp]"
and then using xarg & echo command appending the CDRnames from file a to echo find . -name .
This: x=0 ; x<=${#files[@]} ; x++ is a counter that starts with 0 (x=0) and keeps adding 1 (x++) until the amount of elements in the array is reached (x<=${#files[@]}).
${#files[@]} returns the number of elements in an array called files.
i was trying it in another way
by putting all CDR_ files name inside textfile a.
"[root@test /tmp]cat a
CDR_2010-07-20
CDR_2010-07-12
CDR_2010-08-05
CDR_2010-08-14
CDR_2010-08-11
[root@test /tmp]"
and then using xarg & echo command appending the CDRnames from file a to echo find . -name .
but after this i was finding it impossible to append "-exec grep -H 9718290475" to the existing output.
Hi,
with your cat|xargs approach you will face the same problem that every file will be searched for the search pattern.
So regard this just as complementary info on xargs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.