I have a text file (digit.txt) consisting of one column includes three digit numbers whose range are 001-...365. In the same directory, I have data text files. The file name of these data files as follows, jplg3240.18i. Only the jplg part is constant. I need to order jplg data files w.r.t. the numbers in the digit.txt file using the first 5-7 characters of the file names and store them in another text file. For example:


data files= jplg001.18i.txt, jplg005.18i.txt, jplg301.18i.txt, jplg350.18i.txt,jplg010.18i.txt, jplg365.18i.txt

Output file should be this one:

  • in short you need to cycle on the content of digit.txt and for the only matched data generate a second file with the list of complete filename, is it that correct?
    – AtomiX84
    Jan 8, 2019 at 7:39
  • yes it is true. Jan 8, 2019 at 7:39

2 Answers 2


here the while cycle could be good for reach your goal:

while read element
    if [ -f jplg${element}* ]; then
        echo jplg${element}* >> datafiles.txt
done < digit.txt

Code is supposed to be run in to the path were the files are and give you as per result a file list in the file datafiles.txt with the complete name of the file if was matched in the list in the file digit.txt

  • I think my question was understood slightly wrong. I have lots of data files in the directory and the name of these files starts with jplg. The contents of these data files are not related the jplg string. Jan 8, 2019 at 7:55
  • Update the answer, now only jplg* data will added in the datafiles.txt, anyway was not clear in you original question to me. Sorry for my misunderstanding.
    – AtomiX84
    Jan 8, 2019 at 8:21

If you question is concatenate the data from the files listed in digit.txt then an answer is:

ls jplg* | grep -f digit.txt | xargs cat >out.dat

In slo-mo:

  • ls jplg*: lists a superset of all the files you want (you can make that more restrictive if needed)
  • grep -f digit.txt keeps only the lines that contain one number in digit.txt (this assumes that the three digits don't appear anywhere else in file names)
  • xargs cat: applies cat to all the files listed in the grep output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .