tags:

views:

119

answers:

2

How can I replace a line that starts with "string1" with "string2 lala" using Bash script?

Thanks in advance

+5  A: 
Mimisbrunnr
That does not work... I need to delete all the line that contains "string1", not just replace that part
Neuquino
It supports regex, so the .* should work now. Better?
Mimisbrunnr
It worked, thanks
Neuquino
Note that the g modifier in the second version does nothing because (a) there is only one start to each line and (b) even if the caret were omitted, the first pattern with the '.*' obliterates any other 'string1' appearances on the line.
Jonathan Leffler
Jonathan - You are very right, about the g. It was added in the original suggestion before edits.
Mimisbrunnr
+2  A: 

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
ghostdog74