0

I have to find a file with only a given number and a string. Ito find 856 enclosed with wildcards *856*.

e.g. I have this fileA.txt with thousands of numbers:

45423542354235423542
5423543542353254235
345435245243
435435345
452345345
4523543455454

I have this script:

#!/bin/sh

findFile() {
    while read -r LINE
    do
         printf "$LINE\n"
         grep -il "$LINE" *856*
         printf "\n"
    done < /path/to/fileA.txt > /path/to/result.txt
}

findFile

EDIT: The file names doesn't contain the pattern from fileA.txt.

Desired output:

45423542354235423542
found_file_856_COMPANY_FFW3443E.dat

5423543542353254235
found_file_856_COMPANY_43R43F.dat

345435245243
found_file_856_COMPANY_77Y85HHH.dat

435435345
found_file_856_COMPANY_64Y76H6.dat

452345345
found_file_856_COMPANY_9630GGTFVF.dat

4523543455454
found_file_856_COMPANY_2R98JD925.dat
  • (I can comment now!) Is there unwanted trailing whitespace in your fileA.txt? If so you can trim with CLEANLINE = $( echo "$LINE" | sed 's/[\t ]*$//' ) – eff Oct 11 '17 at 10:36
  • fileA.txt has the numbers needed to find the files I'm looking for. Meaning each number in fileA.txt is equal to one file. Take a look at the example content of fileA.txt. It has six numbers, meaning I'm looking for six files. My script is good, but I'm just stuck with 856 as when you put it inside a script it starts not to read the * or the wildcard. That's my problem now. If I get through that, I'm good to go. – WashichawbachaW Oct 12 '17 at 5:51
  • Did my latest comment on my answer help? Adding the full path sounds like it might be the ticket. – eff Oct 13 '17 at 13:56
  • I decided to call the script from another script. Like I'm doing while loop to execute the script e.g. source /path/to/script.sh pattern is being executed by while loop. It's working now but it could be better. – WashichawbachaW Oct 17 '17 at 8:25
1

Assuming no filename has newlines in them...

Create a list of the "856-files" and use grep to filter out the ones you want:

find . -name '*856*' > filelist
grep -Ff fileA.txt filelist >result.txt

The grep command will use the strings in fileA.txt as patterns and will extract the names from filelist that matches these. The -F flag will ensure that the strings in fileA.txt are interpreted as fixed strings (not as regular expressions).

The patterns in fileA.txt may have to be made more specific, for example by prefixing them with _ and adding the .dat suffix:

sed -e 's/^/_/' -e 's/$/.dat' fileA.txt >fileB.txt

You would then use fileB.txt with the grep instead of fileA.txt.

  • I chose grep because it's way faster than find. I think my only problem in my script is *856* as * is not being read as a wildcard. My script, doesn't output the grep command but it outputs when I type it directly as $ grep -il 'some_pattern_here' *856*. Really need help. I'm doing three thousand to five thousand files to find. It's really hard to do it manually. – WashichawbachaW Oct 12 '17 at 3:36
1

It's not clear what you're asking in your question (i.e. what are you trying to achieve?) or which shell you're using. Sadly I don't have the rep. to comment.

Assuming you're using the bash shell; if you're trying find a globbing pattern which returns all the files in the current folder containing the string 856 then *856* is the correct way.

To clarify, if you want to search for sausages inside all files with name containing the string snacks_for_later then you would use

    $ grep sausages *snacks_for_later*

to find any sausages.


To check that you're searching within the correct files, you can test your globbing pattern using ls:

    $ ls *856*

If you're not running the commands from the folder the files are in, you would have to use the pattern /path/to/allfiles/*856* for both grep and ls. Glob patterns (such as '*') don't traverse directories.

  • It doesn't output the files I'm looking for. I'm using BASH – WashichawbachaW Oct 11 '17 at 9:49
  • Are the files you're looking for in the folder where you execute the script or are they scattered around? – eff Oct 11 '17 at 9:56
  • They are in one directory e.g. /path/to/allfiles/. I only want to do like this command grep -il '452352435' *856* and outputs the file with the 452352435 inside the file. But there are thousands to find and it's really a pain in the ass to do it one by one. – WashichawbachaW Oct 11 '17 at 10:02
  • nothing's happening with ls *snacks* | grep sausages. It doesn't output anything. – WashichawbachaW Oct 11 '17 at 10:17
  • Sorry, that approach was to find files such as "snacks_with_sausages.dat", which you've clarified isn't relevant now. Try ls /path/to/allfiles/*856*. If this works, update the glob pattern in your script to include the '/path/to/allfiles/' bit. – eff Oct 11 '17 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.