【问题标题】:find and replace string in a file在文件中查找和替换字符串
【发布时间】:2011-05-25 04:28:23
【问题描述】:

我正在尝试在文件文件夹中查找和替换字符串。

有人可以帮助我吗?

我的脚本如下:

#!/bin/bash
OLD="This is a"
NEW="I am a"
DPATH="/home/user/test/*.txt"
BPATH="/home/user/test/backup/foo"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
    sed "s/$OLD/$NEW/g" "$f"
   else
    echo "Error: Cannot read $f"
  fi
done

现在这似乎找到了字符串“This is a”并替换为“I am a”,但这只会打印到屏幕上。

我需要它来替换文件本身。

谢谢

【问题讨论】:

    标签: file shell scripting replace


    【解决方案1】:

    由于以下原因,输出进入屏幕 (stdout):

    sed "s/$OLD/$NEW/g" "$f"
    

    尝试重定向到一个文件(以下重定向到一个新文件,然后重命名它以覆盖原始文件):

    sed "s/$OLD/$NEW/g" "$f" > "$f.new" && mv "$f.new" "$f"
    

    【讨论】:

    • 这将创建一个 .text.new 文件,而原始文件 example.txt 被覆盖并且为空白。任何想法为什么?
    • @user390426:这可能是因为 aix 没有引用变量 "$f.new"' (two places) and "$f". Also, the mv` 不应该是无条件的。相反,它应该是sed ... && mv ...
    • 好的,我现在将 $OLD 更改为 URL,它返回错误:sed: 无法打开文件 ww.google.co.uk//g: 没有这样的文件或目录'sed' 命令似乎不喜欢 URL 的 // 部分。有没有办法克服这个问题?
    • @user390426:更改sed命令中的分隔符:"s|$OLD|$NEW|g"
    【解决方案2】:

    这是我使用的 sn-p,它删除当前目录下所有文件中的 APA 和 BEPA 之间的所有内容(跨多行,包括删除 APA、BEPA),不包括 .svn 目录

    find . \! -path '*.svn*' -type f -exec sed -i -n '1h;1!H;${;g;s/APA[ \t\r\n]*BEPA//g;p}' {} \;
    

    【讨论】:

      【解决方案3】:

      使用sed-i 选项进行适当的更改:

      sed -i "s/$OLD/$NEW/g" "$f"
          ^^
      

      【讨论】:

      • 效果很好!我知道我错过了一些东西。谢谢
      • 好的,有没有一种简单的方法可以修改我的 shell 脚本,以便检查子文件夹? /home/user/test/xyz ?
      • #!bin/bash OLD="This is a" NEW="I am a" BPATH="/home/user/backup/foo" find . -name '*.shtml' -type f |同时读取文件名做 /bin/cp -f $filename $BPATH sed -i "s/$OLD/$NEW/g" $filename done
      • 我在尝试使用 -i 时收到此错误:sed: 非法选项 -- i 用法:sed [-n] [-e script] [-f source_file] [file...]
      • @Dchris -i 选项并非完全可移植,尽管它在大多数平台上以某种形式受到支持。在 *BSD(包括 MacOS)上,您需要 -i '';如果您根本没有sed -i,也许可以尝试perl -pi -e ,其中一个简单的sed 脚本可以在Perl 中逐字使用,但请注意正则表达式方言不同(您主要需要了解如何添加反斜杠或在某些地方被删除)。
      【解决方案4】:

      看看这个

      http://cs.boisestate.edu/~amit/teaching/handouts/cs-unix/node130.html

      ##########################################################
      \#!/bin/sh
      
      \# sed/changeword
      
      prog=`basename $0`
      
      case $# in
      0|1) echo 'Usage:' $prog '<old string> <new string>'; exit 1;;
      esac
      
      old=$1
      new=$2
      for f in *
      do
              if test "$f" != "$prog"
      
              then
                  if test -f "$f"
                  then
                      sed "s/$old/$new/g" $f > $f.new
                      mv $f $f.orig
                      mv $f.new $f
                      echo $f done
                  fi
              fi
      done
      
      ##############################################################
      

      【讨论】:

        猜你喜欢
        • 2013-02-19
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-22
        • 2015-09-22
        • 2019-06-24
        相关资源
        最近更新 更多