繁体   English   中英

替换 bash 中的文件/数组内容

[英]Replace file/array content in bash

我有以下两个文件/数组:

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

我想将 $ARG1$ $ARG2$ $FFFS$ 替换为该值。 (并保留 de -l -c -d 等。)

示例输出/预期文件/数组:

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

我尝试了很多东西,但我不知道什么是正确的解决方案。

要处理的文件:

$ 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

假设:

  • $! 仅用作分隔符,这意味着...
  • $! 不应出现在最终输出中
  • 两个文件的行数相同
  • 每对匹配的行具有相同数量的$...$引用和数据值
  • 每对匹配的行在字段 #1 中具有相同的字符串(在“模板”文件中提供了可能的文件扩展名); 最终结果是我不会尝试匹配字段 #1 的内容

一种awk解决方案:

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

注意:删除注释以整理代码。

以上生成:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM