2

So I several large data files that correspond to temperature and vapor in the form tmp_information_about_dataset.file and vap_information_about_dataset.file

So for every tmp_... file there is a corresponding vap_... file, I'll use both datasets to perform some calculations on and then generate a corresponding output file in the form out_information_about_dataset.file

Just to be clear, each pair of tmp/vap files, after information about the datasets (ie any characters after tmp or vap) are identical, so I'll also want the characters after out to be identical to information about the datasets from which it was generated. eg

 tmp_123ABC456_model1_2010.nc
 vap_123ABC456_model1_2010.nc
 out_123ABC456_model1_2010.nc

 tmp_123CDF_model2_2010.nc
 vap_123CDF_model2_2010.nc
 out_123CDF_model2_2010.nc

Is there a way in which I am able to take all the files in the dictionary, perform the calculation on matching pairs and name the output file with an identical file name after the first 3 characters?

I was thinking something like this answer here would work but the fact I need to use to two files with corresponding name, as opposed to any file in the dictionary as well as want separate output files, is throwing me off a bit.

1 Answer 1

2

First, iterate over all tmp_ files:

for file in tmp_*; do

then, for each tmp_ file, you can get the corresponding vap_ file with vap${file#tmp} - first, ${file#tmp} removes the tmp prefix (the variable itself is not modified), then the result is concatenated with vap.

for file in tmp_*; do ./process_stuff "${file}" "vap${file#tmp}" > "out${file#tmp}"; done
3
  • How might this be altered if I wanted to do the same thing, but also change the last part of the file name? For example if I was copying a .txt file and I wanted the identifier to be the same as the corresponding tmp_123.nc.txt file, resulting in tmp_123.txt? I've tried pattern replacement along with mv like this ${tmp//.nc/.txt} but get a syntax error and not sure what the issue is
    – Maitiu
    Jul 12, 2017 at 11:11
  • note, I am not changing the file type here, a .txt file is created from the name of the .nc file, which includes the .nc part, which I want removed
    – Maitiu
    Jul 12, 2017 at 11:12
  • @Maitiu You can use ${tmp#.nc.txt}.txt to remove the .nc.txt from the end and stick just the .txt back
    – Maya
    Jul 12, 2017 at 11:47

You must log in to answer this question.

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