0

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.

2
  • 1
    what are some of the things you've tried and what were the results? assuming the data resides in files, how many lines are in the files? what happens if the number of lines in the 2 files do not match, or the number of arguments for a given line do not match? Commented Nov 21, 2020 at 15:57
  • I have tried play with arrays, sed and cut, but i can't solve. between 5-50 lines are in the files. Every time will match the number of lines and the arguments. Commented Nov 21, 2020 at 16:44

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry if i was not clear. Really thanks the solution! $ARG1$ can change sometime $RANDOMSTRING$ I mean check_jms_queue.sh ASSIGNMENT_IN Queue FFFS 750 There is the FFFS need to be 600. -so $ARG$ could change.
Just the ARG string can change between the $. And it is may leave the number after it. Example: check_jms_queue.sh $ARG1$ $ARG2$ $SOMETING$ $ARG4$
ahhhh, just noticed I missed the $FFFS$ ... hmmmm, will have to think about that a bit more ...
I've updated the answer to completely ignore the strings ARGx in favor of just matching whatever falls between the $ deilmiters which, after splitting the lines on $, means the variable names end up stored in the array elements associated with even-numbered indices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.