3

General Context

I am trying to run several times for different input data a script that uses 2 input files. Not modifying the file pointed out by the script, instead, modifying the content of the same input files, so that the script is untouched.

Particular Context

I am running an Asymptote script (myScript.asy) that takes its data from a pair of files: data.dat and steps.dat and generates a chart.

I need to generate charts for several pairs of files located in several subfolders inside a dataFolder.

The pairs of files are recognizable because they start with the same filename and only differs in the last part, e.g.: 40x214_cores.dat and 40x214_times.dat. They are always in the same subfolder.

The tree is like this:

myScript.asy
data.dat
steps.dat
folderForCharts
dataFolder
  ├ firstSubfolder
  │  ├ AxB_cores.dat
  │  ├ AxB_times.dat
  │  ├ CxD_cores.dat
  │  ├ CxD_times.dat
  │  └ ...
  ├ secondSubfolder
  │  ├ ExF_cores.dat
  │  ├ ExF_times.dat
  │  └ ...
  ├ thirdSubfolder
  │  └ ...

Where A,B,C... are numbers with different length (max: 7 digits).

The Problem

The challenge is to create a loop for bash that:

  • Takes the content of a pair AxB_cores.dat and AxB_times.dat and cat them into data.dat and steps.dat respectively.
  • Runs the asymptote script: asy myScript -o <output filename>, storing the output file in folderForCharts with the name "originSubfolderName_AxB", e.g.: secondSubfolder_54x6789

And then go for the next pair of files covering all of them.

The regex for catching the pairs of files and the subfolder name makes me dizzy, so I have the idea but I don't know how to make it real.
Thanks for give me part of your time, guys.

onlycparra
  • 607
  • 4
  • 22
  • Possible duplicate of http://stackoverflow.com/questions/28725333/looping-over-pairs-of-values-in-bash – tripleee Feb 16 '16 at 07:17
  • Yes and not, because there, he knows the number of files. That the file that have the _1 and _2 are pairs knowing also the beginning (just a number from 4 to 8) , I don't know nothing about the prefix, only that they are equals, not even the number of files in the folder, they are more than 80 I guess – onlycparra Feb 16 '16 at 11:14
  • I added another answer to that question. You should be able to find many other similar questions with good answers. – tripleee Feb 16 '16 at 14:30

1 Answers1

1

Does the following script approximate what you want?

#!/bin/bash

find . -type f -name '*cores.dat' | while read f
do
    g=${f/cores/times}
    if [ -r "$f" -a -r "$g" ] ; then
      dir=$(dirname "$f")
      axb=$(basename "$f")
      axb=${axb/_cores.dat/}
      echo "process $f and $g with dir=$dir and axb=$axb"
    else
      echo "Cannot find $g" >&2
    fi
done
peak
  • 105,803
  • 17
  • 152
  • 177
  • Not in home now, in a couple of hours I'll test it, thanks for your help :) – onlycparra Feb 16 '16 at 11:16
  • it works awesome, it just need a little tweak in the `then` part of the `if` (mentioned in the second point of "The Problem" part of the question): to get in some way the string `subfolderName_AxB`. Where `subfolderName` is the name of the subfolder where the files `$f` and `$g`were found and `AxB` is the common prefix of `$f` and `$g`. How do I get that string? (for the output name of my script). Thanks – onlycparra Feb 17 '16 at 04:50
  • @onlycparra - I've added those extra details. You may also want to modify the `find` command or add a grep filter, depending on the specifics of your case. You also might want to read up a bit on bash ... I think you'll find it well worth the time! – peak Feb 17 '16 at 08:25
  • great, I have made a little modification because I need just the name of the folder without the full path – onlycparra Feb 20 '16 at 02:06