Reputation: 12214
How can I replace a line that starts with "string1" with "string2 lala" using Bash script?
Upvotes: 0
Views: 2385
Reputation: 342363
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
Upvotes: 3
Reputation: 32484
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
Upvotes: 5