views:

98

answers:

4

Hello

I've a text file with a line

default_color acolor

and I want to replace this line with

default_color anothercolor

I don't know the first color and the second is contained in a variable. How can I do it in bash ?

Thank you

+4  A: 

It is not pure bash but if you have other console tools, try something like

cat your_file | sed "s/default_color\ .*/default_color\ $VAR/"
Dmitry
Why do you cat? Just sed it.
ustun
it works ! thank youI just use "sed "s/default_color\ .*/default_color\ $VAR/" file > newfile"
Martin Trigaux
You don't need the redirection if you want to change the file; use the -i switch for that.
ustun
A: 

You could use awk. The manual entry is here:

http://ss64.com/bash/awk.html

I won't write the regular expression necessary, as that will depend on your color format, but this will fit the bill. Best of luck.

TallGreenTree
A: 

use gawk

awk '$2=="defaultcolor"{$2="anothercolor"}1' file

or just bash shell and no external commands

while read -r line
do
 case "$line" in 
   *defaultcolor*) line=${line/defaultcolor/anothercolor}
 esac
 echo $line
done <"file"
ghostdog74
A: 

Not really a bash solution, but you can do the replacement in-place, for any number of files, with perl:

perl -i -npe 's/default_color acolor/default_color another_color/' list_of_files
Idelic