开发者

How to replace a line in bash

How can I replace a line that starts wi开发者_如何学Pythonth "string1" with "string2 lala" using Bash script?


use the sed utility

sed -e 's/^string1.*/string2 lala/'

or

sed -e 's/^string1.*/string2 lala/g'

to replace it every time it appears


using bash,

#!/bin/bash
file="myfile"
while read -r line
do
 case "$line" in
  string1* ) line="string2 lala"
 esac
 echo "$line"
done <"$file" > temp
mv temp $file

using awk

awk '/^string1/{$0="string2 lala"}1' file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜