【问题标题】:grep the filename using sed使用 sed grep 文件名
【发布时间】:2015-07-29 22:50:50
【问题描述】:

我有以下行

result_0.01.dat resultFile

我想获取 (result_0.01) 部分并将其更改为可能包含数字的不同文本。最好我希望使用 sed 来完成。

提前谢谢你。

【问题讨论】:

  • 哦。这是在文本文件中还是什么?我以为这些是文件名。

标签: linux sed


【解决方案1】:
echo "result_0.01.dat resultFile" | sed  "s|.*.dat|someOtherText.dat|g"

在 sed 部分发生的事情是:

we substitude(sed开头的s标志) 我们找到了.dat 之前的所有内容。我们用 someOtherText 替换该部分。 我们在全球范围内进行(最后是 g 标志)

【讨论】:

    【解决方案2】:

    如果要替换文件中的字符串: https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files

    如果你想重命名文件夹中的文件: Using sed to mass rename files

    【讨论】:

      【解决方案3】:
      $ line='result_0.01.dat resultFile'
      $ echo "$line" | sed 's/result_0.01/different text which might include numbers/'
      different text which might include numbers.dat resultFile
      

      【讨论】:

        【解决方案4】:
        echo result_0.01.dat | sed 's/result_0.01/123456/g'
        

        将使用 sed 将 resulat_0.01 替换为 123456

        所以通常你可以这样使用它:sed 's/PATTERN-YOU-WANT-TO-REPLACE/REPLACE-IT-WITH/g'

        【讨论】:

          猜你喜欢
          • 2011-07-29
          • 2012-07-11
          • 2013-06-22
          • 2017-11-09
          • 1970-01-01
          • 2021-05-01
          • 2021-12-13
          • 1970-01-01
          • 2014-04-23
          相关资源
          最近更新 更多