sed - dynamic search and replace

Hi all,

I have a data file formatted as in the following line:

Achadd 0:35 1:35 2:35 3:40 4:40 5:40

I need the minutes converted to seconds; I wrote a script, min2sec, to do so for one datapoint. I was hoping to use sed as in the following code to call this script and reformat the line:

cat datafile.txt | sed 's/\([0-9]*:[0-9]*\)/ $((min2sec \1))/g'

Unfortunately, sed is not calling min2sec. Does anyone have any pointers? I have little experience with sed.

Thanks!

cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/ $(min2sec \1)/g"

Change the quotes and use `min2sec \1` or $(min2sec \1)

Thanks for the reply. (Where's the "duh!" emoticon when I need one?)

There is still a problem, however.

cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/`min2sec \1`/g"

the shell does now execute `min2sec \1`, however it does so before sed substitutes "\1" (before sed is even called, I believe). IOW sed is called as in the following line:

cat datafile.txt | sed "s/\([0-9]*:[0-9]*\)/`60`/g"

Do you see any way arround this problem? Is there any way to tell sed to execute a shell command? Or do I need to look for a solution outside of sed?

Thanks!

I looked to awk to solve my problem instead. The following code converts lines 1-4 into lines 5-7, which was the application I need. I'll just post the code here in case it helps anyone.

  1. Brd02_11_12_18_31

  2. conc 10^{-24} 10^{-23} 10^{-22} 10^{-21} 10^{-20} 10^{-19} 10^{-18}

  3. Achadd 1:00 1:45 3:00 4:00 5:15 6:15 7:15

  4. Remove 2:45 3:45 4:45 6:00 7:00

  5. conc = [ 10^{-24} 10^{-23} 10^{-22} 10^{-21} 10^{-20} 10^{-19} 10^{-18} ];

  6. Achadd = [ 60 105 180 240 315 375 435 ];

  7. Remove = [ 165 225 285 360 420 ];

awk -f fdataf.awk board=Brd02_11_12_18_31 < timedata.txt

where fdataf.awk is the following:

$1 == board, stop == 3 {
   if ($1 != board)
   {
   printf "%s = [ ", $1;
      for (i = 2; i <= NF; i++) {
        n = split($i,x,":");
        if ( n == 1 ) {
          printf "%s ", $i;
       }
       else
       {
         printf  "%d ", x[1]*60 + x[2];
       }
     }
     printf "];\n"
   }
   stop++ 
}

I'm still interested in knowing whether it's possible to call a shell script from within awk or sed, however the above work-arround solves my problem for now. Thanks for the help.

I think it is not possible to send matched pattern ( \1,\2,.. ) to shell script.