开发者

How to replace a line in bash

开发者 https://www.devze.com 2022-12-22 17:22 出处:网络
How can I replace a line that starts wi开发者_如何学Pythonth \"string1\" with \"string2 lala\" using Bash script?use the sed utility

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

精彩评论

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