Is "2nd level indirection" possible in bash?

Walter Dnes waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org
Thu Aug 30 17:48:26 UTC 2012


  I'm trying to do the same bash processing on 3 text files with
different formats.  The processing is linear.  The main loop is...
   while read
   do
      dataline=${REPLY}
# do a bunch of processing
   done < input.txt

  Everything is the same, except the column numbers of the various items
I'm processing.  I don't want to keep 3 copies of basically the same
file, with column numbers changed.  Here's my "Plan A";

* set up 3 separate "format files", e.g...

f_yr="25:4"
f_mo="32:2"
f_dy="37:4"
f_data="50:5"

* "source" the appropriate "format file" for the text file I'm
  processing, and use those values in the script.  Here's one function
  as an example

#
# Routine to assemble date in YYYYMMDD format, using column locations
# imported in format file (i.e. 2nd parameter on commandline)
calc_yyyymmdd() {
   yyyymmdd="${dataline:${f_yr}}${dataline:${f_mo}}${dataline:${f_dy}}"
   export yyyymmdd
}

  This does not work.  Is "2nd level indirection" allowed in bash?  My
"Plan B" is a heavy-handed hack...

* Keep a master copy of the script with lines like...

   yyyymmdd="${dataline:f_yr}${dataline:f_mo}${dataline:f_dy}"

* Replace the "format files" with scripts that run sed, and generate a
  temporary version to do the processing.  E.g...

#!/bin/bash
sed "s/f_yr/25:4/
s/f_mo/32:2/
s/f_dy/37:4/
s/f_data/50:5/" fluxmaster.txt > f1
chmod 744 f1

* This changes the example script line to...

   yyyymmdd="${dataline:25:4}${dataline:32:2}${dataline:37:4}"

* Then run the temporary copy (i.e. f1).

  Any ideas/suggestions/improvements?

-- 
Walter Dnes <waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org>
I don't run "desktop environments"; I run useful applications
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list