通过文件迭代并使用SED替换旧值



im试图编写一个脚本,在该脚本中,wier循环逐行读取文件,在该行上的两个值(要获得两个新值),然后替换两个值,然后替换两个值带有两个新值的旧值,然后移至下一行。

例如,txt文件example.txt包含以下数据:

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

我运行以下脚本:

 #!/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

我希望现在在示例中看到的。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

等等。

当我使用 bash -x Interface.sh im运行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 

请帮忙!不知道如何解决此问题!

读取命令可以读取将行分开为字段:

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

当您不需要在循环内看到当前日期/提议的date时,您可以将循环外的输出重定向以获得更好的性能:

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}

注意:考虑使用$(H2E ...)而不是`

使用批处理(有其他方法)

#!/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不起作用,需要文件
  • 我使用临时文件来避免读取在循环中经常会产生意外结果(如空文件)
  • 的相同文件的问题
  • 这项为您的样本工作,但这取决于您的数组的线结构,并在数据文件的真实结构中替换

相关内容

  • 没有找到相关文章

最新更新