ZoliloZ
ZoliloZ

Reputation: 59

Replace file/array content in bash

I have the following two files/array:

check_appserver.sh $ARG1$ $ARG2$
check_jms_queue.sh $ARG1$ $ARG2$ $FFFS$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh -l $ARG1$ -c $ARG2$ -d $ARG3$ -f $ARG4$

check_appserver!op!95
check_jms_queue!ASSIGNMENT_IN!Queue!600!750
check_jms_queue!OUT!Topic!600!750
check_jms_queue!OUT!Queue!600!750
check_jms_queue!Topic!Topic!5000!10000
check_jms_queue!PL_OUT!Topic!600!750
check_jms_queue!PS_IN!Queue!600!750
check_jms_queue!REJECTED!Queue!600!750
check_jms_queue!GROUND_EVENT_IN!Queue!600!750
check_jms_queue!EM_IN!Queue!600!750
check_jms_queue!VT_IN!Queue!600!750
check_jms_queue!TAIL_IN!Queue!600!750
check_jms_queue!IN!Queue!600!750
check_jms_queue!REJECTED!Queue!600!750

I would like to replace the $ARG1$ $ARG2$ $FFFS$ to the value. (And keep de -l -c -d, etc..)

Example output/expected file/array:

check_appserver.sh op 95
check_jms_queue.sh ASSIGNMENT_IN Queue 600 750

...

check_jms_queue.sh -l REJECTED -c Queue -d 600 -f 750

I have tried lot of thing, but i have no idea what is the right solution.

Upvotes: 0

Views: 128

Answers (1)

markp-fuso
markp-fuso

Reputation: 35336

Files to be processed:

$ cat replace.template
check_appserver.sh $ARG1$ $ARG2$
check_jms_queue.sh $ARG1$ $ARG2$ $FFFS$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh $ARG1$ $ARG2$ $ARG3$ $ARG4$
check_jms_queue.sh -l $ARG1$ -c $ARG2$ -d $ARG3$ -f $ARG4$

$ cat replace.dat
check_appserver!op!95
check_jms_queue!ASSIGNMENT_IN!Queue!600!750
check_jms_queue!OUT!Topic!600!750
check_jms_queue!OUT!Queue!600!750
check_jms_queue!Topic!Topic!5000!10000
check_jms_queue!PL_OUT!Topic!600!750
check_jms_queue!PS_IN!Queue!600!750
check_jms_queue!REJECTED!Queue!600!750
check_jms_queue!GROUND_EVENT_IN!Queue!600!750
check_jms_queue!EM_IN!Queue!600!750
check_jms_queue!VT_IN!Queue!600!750
check_jms_queue!TAIL_IN!Queue!600!750
check_jms_queue!IN!Queue!600!750
check_jms_queue!REJECTED!Queue!600!750

Assumptions:

  • $ and ! are used solely as delimiters which means ...
  • $ and ! should not show up in the final output
  • both files have the same number of lines
  • each matching pair of lines has the same number of $...$ references and data values
  • each matching pair of lines has the same string in field #1 (with a possible file extension provided in the 'template' file); net result is that I won't be attempting to match the contents of field #1

One awk solution:

awk '

# process first file (NR==FNR); store data values in our args[] array

NR==FNR { cnt[FNR]=split($0,arr,"!") -1       # split line on "!" delimiter; place fields in array arr[]; save number of arr[] elements (minus 1) in cnt[] array
          for ( i=1 ; i<=cnt[FNR] ; i++ )     # store our data values in args[] array for later use
              { args[FNR,i]=arr[i+1] }        # eg: first row, first arg is the 2nd element from the arr[] array => args[1,1]=arr[2]
          next                                # process next row from first file
        }

# process second file; replace "$ARGN$" strings with values from args[] aray

    { split($0,arr,"$")                       # split line on "$" delimiter; place fields in array arr[]
                                              # the arguments ($...$) we are interested in replacing will end up in the evenly numbered index entries in arr[]
      for ( i=1 ; i<=cnt[FNR] ; i++ )         # for each data value from the args[] array ...
          { gsub(arr[i*2], args[FNR,i]) }     # replace the matching variable (arr[i*2]); eg, replace 1st "$<arg>$" (arr[2]) with 1st value (args[1]), replace 2nd "$<arg>$" (arr[4]) with 2nd value (args[2])
      gsub(/\$/,"")                           # remove all "$" from the current line
      print                                   # print the modified line
    }

' replace.dat replace.template

NOTE: Remove comments to declutter code.

The above generates:

check_appserver.sh op 95
check_jms_queue.sh ASSIGNMENT_IN Queue 600 750
check_jms_queue.sh OUT Topic 600 750
check_jms_queue.sh OUT Queue 600 750
check_jms_queue.sh Topic Topic 5000 10000
check_jms_queue.sh PL_OUT Topic 600 750
check_jms_queue.sh PS_IN Queue 600 750
check_jms_queue.sh REJECTED Queue 600 750
check_jms_queue.sh GROUND_EVENT_IN Queue 600 750
check_jms_queue.sh EM_IN Queue 600 750
check_jms_queue.sh VT_IN Queue 600 750
check_jms_queue.sh TAIL_IN Queue 600 750
check_jms_queue.sh IN Queue 600 750
check_jms_queue.sh -l REJECTED -c Queue -d 600 -f 750

Upvotes: 1

Related Questions