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
精彩评论