简体   繁体   English

遍历文件并使用sed将旧值替换为新值

[英]Iterating through a file and replacing old values with new values using sed

Im trying to write a script in which a while loop reads a file line by line, performs a command on two values on that line (to get two new values), then replaces the two old values with the two new values, then moves on to the next line. 我试图编写一个脚本,其中while循环逐行读取文件,对该行上的两个值执行命令(以获取两个新值),然后用两个新值替换两个旧值,然后继续到下一行。

For example the txt file example.txt contains the following data: 例如,txt文件example.txt包含以下数据:

 1432771200 != 1432800000 OPTION VALUE
 1432771200 != 1432800210 OPTION VALUE
 1432771200 != 1432800033 OPTION VALUE

And I run the following script: 然后运行以下脚本:

 #!/bin/bash -x
 #
    while read line
    do
            arr=($line)
            CURRENTDATE=`h2e ${arr[0]}`
            PROPOSEDDATE=`h2e ${arr[2]}`
            echo $CURRENTDATE
            echo $PROPOSEDDATE
    #       echo $line |
            sed -i "s/${arr[0]}/$CURRENTDATE/"
    #       echo $line |
            sed -i "s/${arr[2]}/$PROPOSEDDATE/"
    done < /srg/pro/data/example.txt

What I expect to see now in example.txt file is the replacement of the first and third value on each line.. so it should look like this: 我现在希望在example.txt文件中看到的是每行上的第一个和第三个值的替换..因此它看起来应该像这样:

 Thu May 28 01:00:00.000 2015 BST != Thu May 28 09:00:00.000 2015 BST OPTION VALUE
 Thu May 28 01:00:00.000 2015 BST != Wed May 27 01:00:00.000 2015 BST OPTION VALUE
 Fri May 28 01:00:00.000 2015 BST != Fri May 29 06:00:00.000 2015 BST OPTION VALUE

And so on and so forth.. 等等等等..

When I run the shell script with bash -x Interface.sh Im getting this: 当我使用bash -x Interface.sh运行shell脚本时,我得到了这个:

 read line
 + arr=($line)
 ++ h2e 1432771200
 + CURRENTDATE='1432771200.000  2015148  Thu May 28 01:00:00.000 2015 BST (1)'
 ++ h2e 1432800000
 + PROPOSEDDATE='1432800000.000  2015148  Thu May 28 09:00:00.000 2015 BST (1)'
  + echo 1432771200.000 2015148 Thu May 28 01:00:00.000 2015 BST '(1)'
   1432771200.000 2015148 Thu May 28 01:00:00.000 2015 BST (1)
   + echo 1432800000.000 2015148 Thu May 28 09:00:00.000 2015 BST '(1)'
   1432800000.000 2015148 Thu May 28 09:00:00.000 2015 BST (1)
 + sed -i 's/1432771200/1432771200.000  2015148  Thu May 28 01:00:00.000       2015 BST (1)/'
 sed: no input files
 + sed -i 's/1432800000/1432800000.000  2015148  Thu May 28 09:00:00.000   2015 BST (1)/'
 sed: no input files
 + read line
 + arr=($line)
 ++ h2e 1432771200
 + CURRENTDATE='1432771200.000  2015148  Thu May 28 01:00:00.000 2015 BST (1)'
 ++ h2e 1432800000
+ PROPOSEDDATE='1432800000.000  2015148  Thu May 28 09:00:00.000 2015 BST  (1)'
 + echo 1432771200.000 2015148 Thu May 28 01:00:00.000 2015 BST '(1)'
 1432771200.000 2015148 Thu May 28 01:00:00.000 2015 BST (1)
 + echo 1432800000.000 2015148 Thu May 28 09:00:00.000 2015 BST '(1)'
 1432800000.000 2015148 Thu May 28 09:00:00.000 2015 BST (1)
 + sed -i 's/1432771200/1432771200.000  2015148  Thu May 28 01:00:00.000 2015 BST (1)/'
 sed: no input files
+ sed -i 's/1432800000/1432800000.000  2015148  Thu May 28 09:00:00.000 2015 BST (1)/'
sed: no input files 

Please help! 请帮忙! Dont know how to fix this! 不知道该如何解决!

Using your batch (there are other way) 使用您的批次(还有其他方法)

#!/bin/bash -x
 #
  cp /srg/pro/data/example.txt /tmp/temp.file

  cat /srg/pro/data/example.txt \
  | while read line
    do
            arr=($line)
            CURRENTDATE=`h2e ${arr[0]}`
            PROPOSEDDATE=`h2e ${arr[2]}`
            echo $CURRENTDATE
            echo $PROPOSEDDATE
    #       echo $line |
            sed -i "s/${arr[0]}\(.*\)${arr[2]}/${CURRENTDATE}\1${PROPOSEDDATE}/" /tmp/temp.file
    done

mv /tmp/temp.file /srg/pro/data/example.txt 
  • sed -i does not work on stream, need a file sed -i在流上不起作用,需要一个文件
  • I use a temporary file to avoid problem reading the same file that is modified inside the loop that often create unexpected result (like empty file) 我使用一个临时文件来避免读取经常在循环中创建的文件,而该文件经常会产生意外的结果(例如空文件)
  • this work for your sample but it depend of line structure for your array and sed substitution in real structure of data file 这适用于您的示例,但它取决于数组的行结构和数据文件实际结构中的sed替换

The read command can read split the line into fields: read命令可以读取将行拆分为字段:

outfile=/tmp/some_tmp_file_you_will_move
while read date1 marker date2 otherfields; do
    CURRENTDATE=`h2e ${date1}`
    PROPOSEDDATE=`h2e ${date2}`
    echo $CURRENTDATE
    echo $PROPOSEDDATE
    echo "${CURRENTDATE} ${marker} ${PROPOSEDDATE} ${otherfields}" >> ${outfile}
done < /srg/pro/data/example.txt

When you don't need to see the CURRENTDATE/PROPOSEDDATE inside your loop, you can redirect the output outside the loop for a better performance: 当您不需要在循环内看到CURRENTDATE / PROPOSEDDATE时,可以将输出重定向到循环外以提高性能:

outfile=/tmp/some_tmp_file_you_will_move
while read date1 marker date2 otherfields; do
    CURRENTDATE=`h2e ${date1}`
    PROPOSEDDATE=`h2e ${date2}`
    echo "${CURRENTDATE} ${marker} ${PROPOSEDDATE} ${otherfields}"
done < /srg/pro/data/example.txt > ${outfile}

Note: Consider using $(h2e ...) and not `` 注意:考虑使用$(h2e ...)而不是``

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

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