0

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:

digit.txt=
005
301
350
005
010

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:

output=
jplg005.18i
jplg301.18i
jplg350.18i
jplg005.18i
jplg010.18i
  • 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 at 7:39
  • yes it is true. – deepblue_86 Jan 8 at 7:39
2

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

while read element
do
    if [ -f jplg${element}* ]; then
        echo jplg${element}* >> datafiles.txt
    fi
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. – deepblue_86 Jan 8 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 at 8:21
0

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

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.