在目录中的所有文件中查找并替换具有增量值的字符串

时间:2015-09-19 18:02:16

标签: xml replace awk sed

我能够在文件中替换但是使用awk命令无法正确地增加值(添加行号),并且还需要对目录中的所有文件执行此操作。

awk '{ sub(/ID>NA/,"ID>NA"++n); print }' file1 >file1

file.xml

<ID>NA</ID>
<Subject>ASDF</Subject>
 ...
<ID>NA</ID>
<Subject>ASDF</Subject>
...
<ID>NA</ID>
<Subject>ASDF</Subject>
...
<ID>NA</ID>
<Subject>ASDF</Subject>

预期结果是:

file1.xml

<ID>NA1</ID>
<Subject>ASDF</Subject>
...
<ID>NA2</ID>
<Subject>ASDF</Subject>
...
<ID>NA3</ID>
<Subject>ASDF</Subject>
...
<ID>NA4</ID>
<Subject>ASDF</Subject>

2 个答案:

答案 0 :(得分:1)

这可能是你想要的:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="GUIDE">
    <meta name="author" content="">

    <title>NYC GUIDE</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/custom.css" rel="stylesheet">





  </head>

  <body>


    <div class="row" id="navbar">
      <div class="col-xs-12 col-sm-4">
        <div class="btn-group btn-group-justified filters">
          <a href="#" class="labelz label-eat btn active" data-value="Eat">Eat</a>
          <a href="#" class="labelz label-chill btn active" data-value="chill">Chill</a>
          <a href="#" class="labelz label-drink btn active" data-value="drink">Drink</a>
          <a href="#" class="labelz label-fashion btn active" data-value="fashion">Fashion</a>

        </div>
      </div>

    </div>

    <div class="container" id="places-wrapper">
      <div class="row places all-places">
        <div class="col-sm-4 place drink eat">
          <h5 class="tags"><span class="label label-drink">Drink</span><span class="label label-eat">Eat</span></h5>
          <h3>Place 1</h3>

        </div>
        <div class="col-sm-4 place fashion">
          <h5 class="tags"><span class="label label-fashion">Fashion</span></h5>
          <h3>Place 2</h3>
        </div>

        <div class="col-sm-4 place fashion">
          <h5 class="tags"><span class="label label-chilll">chill</span></h5>
          <h3>Place 3</h3>
        </div>

    </div>



    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/app.js"></script>
    <script async defer
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA6eMmlCy8MX-p6Imo0scPRWj6D0MCl1y0&&callback=initMap">
    </script>
  </body>
</html>

但你的问题并不是100%清楚。使用GNU awk 4. *您可以将上述内容缩写为:

for file in *
do
    awk '{ sub(/ID>NA/,"ID>NA"++n); print }' "$file" > tmp &&
    mv tmp "$file"
done

答案 1 :(得分:1)

您每行都在增加n。从示例输出中,您似乎只想在进行替换的行上增加$ awk '/ID>NA/{ sub(/ID>NA/,"ID>NA"++n)}; 1' file1.xml <ID>NA1</ID> <Subject>ASDF</Subject> ... <ID>NA2</ID> <Subject>ASDF</Subject> ... <ID>NA3</ID> <Subject>ASDF</Subject> ... <ID>NA4</ID> <Subject>ASDF</Subject> 。在那种情况下:

awk ... file > file

此外,$ awk '/ID>NA/{ sub(/ID>NA/,"ID>NA"++n)}; 1' file1.xml >tmp && mv tmp file1.xml 无法用于更改文件。使用

awk -i inplace '/ID>NA/{ sub(/ID>NA/,"ID>NA"++n)}; 1' *.xml

使用GNU awk,可以简化。要一次更改目录中的所有xml文件:

private int myProperty;

    public int MyProperty
    {
        get { return myProperty; }
        set
        {
            Set(ref myProperty, value);
        }
    }