使用带有sed的bash脚本修改文件

时间:2016-11-14 02:22:10

标签: linux bash sed

类似于这些问题:

在这样的文件中:

define('_DB_NAME_', 'anything');
define('_DB_USER_', 'something else');
define('_DB_PASSWD_', 'and another value');

如何通过引用键值anythingsomething来使用sed替换值another_DB_NAME__DB_USER_(未知)和_DB_PASSWD

3 个答案:

答案 0 :(得分:1)

你可能想要:

sed -E "
  s/('_DB_NAME_', ')[^']+/\1ABC/
  s/('_DB_USER_', ')[^']+/\1DEF/
  s/('_DB_PASSWD_', ')[^']+/\1GHI/
"

我认为awk更具可读性:

awk -F "'" -v OFS="'" '
    $2 == "_DB_NAME_"   {$4 = "ABC"}
    $2 == "_DB_USER_"   {$4 = "DEF"}
    $2 == "_DB_PASSWD_" {$4 = "GHI"}
    {print}
'

答案 1 :(得分:1)

sed "s/\(define('_DB_NAME_', \).*/\1'new value');/" filename

同样适用于其他两个。

如果要更改文件,可以使用-i,但必须小心,不同版本的sed处理方式不同。

答案 2 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<nav class="icone-menu">
  <a href="index.html">
    <img src="img/logo.png" alt="crossagenda" class="logo-mobile">
  </a>
  <div class="container" onclick="animaIcon(this), myFunction(this)">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
    <div id="myDropdown" class="dropdown-content">
      <ul>
        <li><a href="#">Home</a>
        </li>
        <li><a href="#">What</a>
        </li>
        <li class="down-botao"><a href="#">Download</a>
        </li>
      </ul>

    </div>
  </div>
  <div class="modal">
    <div class="tab">
      <input type="radio" id="sign-in" name="options" class="option" checked />
      <label for="sign-in">Sign in</label>
      <form action="#" class="form">
        <p class="float-label">
          <label for="username">Username</label>
          <input type="text" id="username" name="username" placeholder="Username" />
        </p>
        <p class="float-label">
          <label for="password">Password</label>
          <input type="password" id="password" name="password" placeholder="Password" />
        </p>
        <p>
          <input type="submit" value="Sign me in" />
        </p>
        <div class="forgot">
          <a href="#">Forgot your password?</a>
        </div>
      </form>
    </div>
    <div class="tab">
      <input type="radio" id="sign-up" name="options" class="option" />
      <label for="sign-up">Sign up</label>
      <form action="#" class="form">
        <p class="float-label">
          <label for="email">Email</label>
          <input type="email" id="email" name="email" placeholder="Email" />
        </p>
        <p class="float-label">
          <label for="password">Password</label>
          <input type="password" id="password" name="password" placeholder="Password" />
        </p>
        <p class="float-label">
          <label for="confirm-password">Confirm password</label>
          <input type="password" id="confirm-password" name="confirm-password" placeholder="Confirm password" />
        </p>
        <p class="toggle">
          <label for="accept">
            <input type="checkbox" id="accept" name="accept" />I accept the <a href="#">Terms &amp; Services</a>
          </label>
        </p>
        <p>
          <input type="submit" value="Sign me up" />
        </p>
      </form>
    </div>
  </div>
</nav>

其中tempfile包含您拥有的代码。输出看起来像这样

sed -i -e 's/anything/newanything/g;s/something/newthing/g;s/another/newanother/' tempfile