LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2010, 02:07 AM   #1
shellarchive
Member
 
Registered: Jan 2007
Posts: 50

Rep: Reputation: 15
find script


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.
Old 08-18-2010, 02:17 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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
Hope this helps.
 
Old 08-18-2010, 02:41 AM   #3
shellarchive
Member
 
Registered: Jan 2007
Posts: 50

Original Poster
Rep: Reputation: 15
i really appreciate your quick reply.

however what i want is to search first number say 9990035233 in first cdr file ie CDR_2010-07-20.

in simpler words, script should execute command like this,

find /data6/ -name CDR_2010-07-20 -exec grep -H 9990035233 {} \;
find /data6/ -name CDR_2010-07-12 -exec grep -H 9718290475 {} \;
find /data6/ -name CDR_2010-08-05 -exec grep -H 9899867969 {} \;

and so on.
 
Old 08-18-2010, 03:06 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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
Hope this helps.

Last edited by druuna; 08-18-2010 at 03:07 AM.
 
2 members found this post helpful.
Old 08-18-2010, 03:07 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by shellarchive View Post
i really appreciate your quick reply.

however what i want is to search first number say 9990035233 in first cdr file ie CDR_2010-07-20.

in simpler words, script should execute command like this,

find /data6/ -name CDR_2010-07-20 -exec grep -H 9990035233 {} \;
find /data6/ -name CDR_2010-07-12 -exec grep -H 9718290475 {} \;
find /data6/ -name CDR_2010-08-05 -exec grep -H 9899867969 {} \;

and so on.
Hi,

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.
Code:
find /data6/ -name 'CDR_2010-07-20*' -exec grep -H 9990035233 {} \;
find /data6/ -name 'CDR_2010-07-12*' -exec grep -H 9718290475 {} \;
find /data6/ -name 'CDR_2010-08-05*' -exec grep -H 9899867969 {} \;
Or do you want a script, where you pass the files and patterns as parameters? E. g. something like

./search.sh file1 pattern1 [file2 pattern]
Code:
#!/bin/bash

while [[ $# -ge 2 ]]; do
    find /data6/ -name "$1" -exec grep -H "$2" {} \;
    shift
    shift
done
(UNTESTED)
 
2 members found this post helpful.
Old 08-18-2010, 03:57 AM   #6
shellarchive
Member
 
Registered: Jan 2007
Posts: 50

Original Poster
Rep: Reputation: 15
thanks druuna & crts for quick solution.

both the script did the work.

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 .

"[root@test /tmp]cat a|xargs -n1 echo "find /data6/ -name "
find /data6/ -name CDR_2010-07-20
find /data6/ -name CDR_2010-07-12
find /data6/ -name CDR_2010-08-05
find /data6/ -name CDR_2010-08-14
find /data6/ -name CDR_2010-08-11
"

but after this i was finding it impossible to append "-exec grep -H 9718290475" to the existing output.
 
Old 08-18-2010, 04:07 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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.

Have a look here: Advanced Bash-Scripting Guide - Chapter 27. Arrays for more details on arrays in bash.

Hope this clears things up a bit.

BTW: You're welcome
 
1 members found this post helpful.
Old 08-18-2010, 04:31 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by shellarchive View Post
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 .

"[root@test /tmp]cat a|xargs -n1 echo "find /data6/ -name "
find /data6/ -name CDR_2010-07-20
find /data6/ -name CDR_2010-07-12
find /data6/ -name CDR_2010-08-05
find /data6/ -name CDR_2010-08-14
find /data6/ -name CDR_2010-08-11
"

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.

Try the following
Code:
cat a | xargs -n1 -I [] echo "find /data6/ -name [] -exec grep 'pattern' {} \;"

Last edited by crts; 08-18-2010 at 04:33 AM.
 
1 members found this post helpful.
Old 08-18-2010, 05:43 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
you can put everything in pairs like this one

input file:
Code:
CDR_2010-07-20:9990035233
CDR_2010-07-12:9718290475
CDR_2010-08-05:9899867969
CDR_2010-08-14:9820048711
CDR_2010-08-11:9830589910
script:
Code:
#!/bin/bash
while read; do
    A=${REPLY%:*} B=${REPLY#*:}
    find /data6/ -name "$A" -exec grep -H "$B" {} \;
done
run:
Code:
bash script.sh < input_file.list
Same concept can be made if you want to add custom target directories instead of just data6.

Last edited by konsolebox; 08-18-2010 at 05:45 AM.
 
Old 08-18-2010, 05:50 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Or in separate files:
Code:
#!/bin/bash

LIST1=$1 LIST2=$2

if [[ -f $LIST1 && -f $LIST2 ]]; then
    while read -u 3 A && read -u 4 B; do
        find /data6/ -name "$A" -exec grep -H "$B" {} \;
    done 3<"$LIST1" 4<"$LIST2"
fi
Code:
bash script.sh input_file_a.list input_file_b.list
Same concept can be applied for 3 file list.
 
1 members found this post helpful.
  


Reply



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
Can't find moblock's script jena Linux - Software 1 01-13-2008 11:13 PM
Shell script: not able to find jags.singh Programming 8 06-15-2007 10:12 AM
find script kaiserbeto Linux - Newbie 4 12-02-2006 02:49 AM
find shell script help liren Linux - Newbie 3 05-02-2005 03:05 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 AM.

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
Open Source Consulting | Domain Registration