Facundo Ch.
Facundo Ch.

Reputation: 12214

How to replace a line in bash

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

Upvotes: 0

Views: 2385

Answers (2)

ghostdog74
ghostdog74

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

zellio
zellio

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

Related Questions