Bash snippet to find files based on a text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash snippet to find files based on a text file?
# 1  
Old 04-01-2009
Question Bash snippet to find files based on a text file?

Evening all. I'm having a terrible time with a script I've been working on for a few days now...

Say I have a text file named top10song.tm2, with the following in it:

Code:
kernkraft 400
Imagine
i kissed a girl
Thriller
animals
hallelujah
paint it black
psychosocial
Oi to the world
Zombie Zoo
song that never ends

The part of my script I'm having trouble with is as follows:

Code:
while read title
do
  title=$(echo $title | tr '[A-Z' '[a-z]')
  find /mnt/music -iname "*$title*" >> top10.lst
done < top10song.tm2

I've confirmed that every title in the top10song.tm2 list exists somewhere in /mnt/music. Running /find /mnt/music -iname "*paint it black*" from the command line returns a positive match, for example. But in the script? Nothing.

Also, is there a way to get find to stop after the first match?

Thanks in advance!
# 2  
Old 04-02-2009
Quote:
Originally Posted by DJ Charlie
Evening all. I'm having a terrible time with a script I've been working on for a few days now...

Say I have a text file named top10song.tm2, with the following in it:

Code:
kernkraft 400
Imagine
i kissed a girl
Thriller
animals
hallelujah
paint it black
psychosocial
Oi to the world
Zombie Zoo
song that never ends

The part of my script I'm having trouble with is as follows:

Code:
while read title
do
  title=$(echo $title | tr '[A-Z' '[a-z]')
  find /mnt/music -iname "*$title*" >> top10.lst
done < top10song.tm2

I've confirmed that every title in the top10song.tm2 list exists somewhere in /mnt/music. Running /find /mnt/music -iname "*paint it black*" from the command line returns a positive match, for example. But in the script? Nothing.

Also, is there a way to get find to stop after the first match?

Thanks in advance!
Try this:
for title in $(<top10song.tm2)
do
title=$(echo $title | tr '[A-Z' '[a-z]')
find /mnt/music -iname "*$title*" >> top10.lst
done
# 3  
Old 04-02-2009
Quote:
Originally Posted by siquadri
Try this:
for title in $(<top10song.tm2)
do
title=$(echo $title | tr '[A-Z' '[a-z]')
find /mnt/music -iname "*$title*" >> top10.lst
done
Well, partial success.

Now it seems the trouble is that when the search string contains more than one word (like zombie zoo from the example file), it searches separately on each word. And to make matters worse, it's sending EVERY match to top10.lst

Any suggestions on making it treat each line as a single variable? And I'd still like to find a way for it to just return one match.
# 4  
Old 04-02-2009
while read title
do
title="`echo $title|tr '[A-Z]' '[a-z]`"
find /mnt/music -name "*$title*" >> top10.lst
done < top10song.tm2
# 5  
Old 04-02-2009
Quote:
Originally Posted by siquadri
Try this:
for title in $(<top10song.tm2)
do
title=$(echo $title | tr '[A-Z' '[a-z]')
find /mnt/music -iname "*$title*" >> top10.lst
done

Try this:

for title in $(<top10song.tm2)
do
"title"=$(echo "$title" | tr '[A-Z' '[a-z]')
find /mnt/music -iname "*$title*" >> top10.lst
done
# 6  
Old 04-02-2009
No joy with either of those. It's still splitting each line into words, and running find on each word individually, which results in a few hundred matches each.
# 7  
Old 04-02-2009
Those are back ticks on the title variable line. My solution works I have tested it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

How to find list of missing files based on the file format?

Hi All, In the file names we have dates. Based on the file format given by the user, if any file is not existed for a particular date with in a given interval we should consider that file is missing. I have the below files in the directory /bin/daily/voda_files. ... (9 Replies)
Discussion started by: nalu
9 Replies

3. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

4. Shell Programming and Scripting

Code snippet to cut XML files based on record length

I want to do FTP an Huge XML file to mainframe server using AIX server Since my file size is huge, i want to split the XML file based on a delimiter , the record delimiter should be set after every 27000 bytes of data and then do the ftp This is done becos the data send to the mainframe must... (1 Reply)
Discussion started by: vishwanath001
1 Replies

5. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

7. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

8. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

9. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies

10. Shell Programming and Scripting

splitting files based on text in the file

I need to split a file based on certain context inside the file. Is there a unix command that can do this? I have looked into split and csplit but it does not seem like those would work because I need to split this file based on certain text. The file has multiple records and I need to split this... (1 Reply)
Discussion started by: matrix1067
1 Replies
Login or Register to Ask a Question