简体   繁体   中英

Executing a bash loop script from a file

I am trying to execute this in unix. So let's for example say I have five files named after dates, and in each of those files there are thousand of numerical values (six to ten digit number). Now, lets say I also have bunch of numerical values and I want to know which value belongs to which file.I am trying to do it the hard way like below but how do I put all my values in a file and just do a loop from there.

FILES:

20170101
20170102
20170103
20170104
20170105

Code:

for i in 5555555 67554363 564324323 23454657 666577878 345576867; do 
    echo $i; grep -l $i 201701*; 
done

Or, why loop at all? If you have a file containing all your numbers (say numbers.txt you can find in which date file each are contained and on what line with a simple

grep -nH -w -f numbers.txt 201701*

Where the -f option simply tells grep to use the values contained in the file numbers.txt to search in each of the files matching 201701* . The -nH options for listing the line number and filename associated with each match, respectively. And as Ed points out below, the -w option to insure grep only select lines containing the whole word sought.

You can also do it with a while loop and read from the file if you create it as @Barmar suggested:

while read -r i; do
    ...
done < numbers.txt

Put the values in a file numbers.txt and do:

for i in $(cat numbers.txt); do
    ...
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM