0

multiple files with the same name grid_200001.grb2.nc with only one components changing and that is the month grid_200010.grb2.nc. I would like to use them all simultaneously as input files

An example of what I am hoping to achieve is

fname="grid_"
year="2010"
month="01 02 03 04 05 06 07 08 09 10"
ext="grb2"
end="nc"

for((y=$year;y<=$year;y++));
do
    for m in $month
    do

    ifile=$fname$y$m.$ext.$end

    >>merge $ifile $ifile ... ofile 

example of the desired command i would like to have is

 >> merge grid_200001.grb2.nc grid_200002.grb2.nc ....grid_200012.grb2.nc ofile

I would like all the files i have available as input files inputs at the same time

George
  • 128
  • 3
  • 12
  • It would be better if you give more details about how you want it done as I see many possible intentions from your explanation. It's better not to guess. A pseudocode perhaps would do. – konsolebox Jul 05 '14 at 09:46
  • @konsolebox Thank you for the advice, i re-wrote with an example and hopefully now it is clearer what i want to achieve – George Jul 05 '14 at 17:52
  • `ofile` would be overwritten in every loop. Is that intended? Also, please indent your merging command properly so we can tell which loop body you want to run it. Why as well is `cat` not applicable? `cat ifile1 ifile2 > ofile` – konsolebox Jul 05 '14 at 18:00
  • @konsolebox I am using a specific toolbox which merges the kind of files that i have, cat unfortunately is not something i can use. the objective is to use all the files i have from previous step as simultaneous inputs and produce one final output (ofile) – George Jul 05 '14 at 18:06
  • I understand. Still it's confusing how you get the sources for merging and where you want to merge them. Can you modify your loop body to make it complete (ended with `done`) and -indent- it well so we'd know more how you intend to merge it? As you see your assignment with `ifile` and your `merge` command is aligned with the inner `for` loop. – konsolebox Jul 05 '14 at 18:09
  • @konsolebox Yes they are, sorry about that. Those files are all created in previous steps that used that is why they have similar names with only one element changed. the script is a bit big to be posted here and contains the commands from the toolbox and didn't want to tire everybody with un-necessary commands. I tried to present it here with as much info as possible, this is the final step of my script and i want all of the automated files created in a previous step i did to be inserted as inputs together – George Jul 05 '14 at 18:15

3 Answers3

1
fname="grid_"
years=({2000..2010})
months=(01 02 03 04 05 06 07 08 09 10)  ## ({01..10}) should be the same
ext="grb2"
end="nc"

shopt -s extglob
IFS='|' eval 'pattern="${fname}@(${years[*]})@(${months[*]}).${ext}.${end}"'
echo "pattern = ${pattern}"  ## for curiosity.
files=($pattern)  ## pathname expansion is sorted in Bash.

merge "${files[@]}" ofile
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • To understand the process you use the pattern as "reader" for all the files and you use the wildcard(*) to make sure all the files with the fname are included as inputs (correct??) then in the command of merge i have i can name pattern = file and use them all as simultaneous inputs (?) what does the @ means? – George Jul 05 '14 at 19:18
  • Yes it's a wildcard pattern but an [extended form](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html). `"{files[@]}"` expands properly to multiple arguments. You can do `echo merge "${files[@]}" ofile` to know how it goes with your command. – konsolebox Jul 05 '14 at 19:42
0

You can try this:

cat grid_2010[01][0-9].grb2.nc >> ofile
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
  • I dont want to use cat, I am using a toolbox that requires all the same files to be used as simultaneous inputs. The whole thing is a part of a larger script and this is a step, but I am confused on how can i use the multiple files as input at the same time – George Jul 04 '14 at 11:19
  • @George I don't know what your purposes are, but in your script you're are not taking ".grb2." into consideration, have you noticed that? – Tiago Lopo Jul 04 '14 at 11:22
  • thanks for the notice, i didnt noticed that, i will correct it now. I tried to give the code in simple example and not use my inputs – George Jul 04 '14 at 11:25
0

I'm not sure I understand the question exactly (edited thanks to your comments)

filenames=""

for year in $(seq 2000 2010)
do 
    for month in $(seq -w 0 10)
    do 
        filename="grid_"$year$month".grb2.nc"
        filenames=$filenames" "$filename
    done
done
your_command $filenames

which may be what you need? cat and wildcards is nice but would it keep the same order?

edited again: But then, the wildcard choice is much simpler your_command grid_20[01][0-9][01][0-9].grb2.nc if you need them only in ascending order (will that always be the case with the wildcards?)

Emilien
  • 2,385
  • 16
  • 24
  • As i mention i have the file produced previously in the code. The name of the files is usually long but I gave the example of the code that i want to create to make it easier to read. Unfortunately if i use wildcards at this point i endanger the process of the process, since usually i have form 20-50 files with similar names. – George Jul 04 '14 at 11:31
  • 1
    @Emillien You may use `seq -w 0 10` for padding zeros – Tiago Lopo Jul 04 '14 at 11:37
  • Thanks for the -w tip, it's much nicer! @George I still don't get what you want, do you need to put all these names in a string and in this order such that you can apply on command to all of them? In your question the `merge $ifile ... ofile ` is not clear – Emilien Jul 04 '14 at 11:41
  • @Emilien Yes, i would like to take the names of all the files and put them in ascending order after that the command i use will take care of the rest . – George Jul 04 '14 at 12:00