tags:

views:

279

answers:

5
+2  A: 

Try:

sed -i 's/^DATE:.*$/DATE: XXXX/g' filename

Answer to your 2nd question depends on how often you need to write such things. If it's a one time thing and time is limited it's better to ask here. If not, it's better to learn how to do it by using an online tutorial.

codaddict
it's funny b/c it's exactly what I wanted to answer but when I tried I got DATE: XXXX°c in my file :(
PierrOz
@codaddict: little update: actually your command doesn't modify my file. What gives me the result above is :sed -i s/DATE[^$]*$/"DATE: XXXX"/g filename
PierrOz
A: 

You can use the following code also

> var="DATE: 12 jan 2009, weather 20ºC" 
 > echo $var | sed -r 's!^(DATE:).*$!\1 XXXX!'
muruga
+1  A: 

You can use the shell

while IFS=":" read -r a b
do
  case "$a" in
   DATE* ) echo "$a:XXXX";;
   *) echo "$a$b" ;;
  esac
done < "file"

or awk

$ cat file
some line
DATE: 12 jan 2009, weather 20ºC
some line

$ awk -F":" '$1~/^DATE/{$2="XXXX"}1' OFS=":" file
some line
DATE:XXXX
some line
ghostdog74
A: 

If your line isn't in a file, you can do it without sed :

var="DATE: 12 jan 2009, weather 20ºC"
echo "${var/DATE:*/DATE:xxxxx}"
David V.
+1  A: 
while read line
do
    echo "${line/#DATE*/DATE:xxxxx}"
done < myfile