tags:

views:

65

answers:

5

Hi,

I have the following variable in bash:

var1="this is ok"

and in a file I want fo make in all lines the subst:

_day 23. 45. 56. ...

to

this is ok_day 23. 45. 56. 

so I add var1 to the start of the line

I tried with this

sed -e "s/_/${var1}"_"/g" ${filename}

but i get errors

I do not know if this is because the spaces. I aslo chaged / to | on sed but nothing

any hints?

Thanks

+1  A: 

The subst pattern you gave will not put $var1 at the beginning of the line:

sed -e "s/^/$var1/g" "${filename}"
Ignacio Vazquez-Abrams
it does not work
asdf
It should: I tried it successfully.
mouviciel
A: 
var1="this is ok"; 
echo _day 23. 45. 56. | 
  sed "s/^/${var1}/"

outputs

this is ok_day 23. 45. 56.

Alternatively, use awk (no messing around with regexps, which aren't required)

echo _day 23. 45. 56. |
  awk '{print "'"$var1"' " $1}' < abc.txt
Alex Brown
+1  A: 

you can just use the shell

#!/bin/bash
var1="this is ok"
while read -r line
do
    case "$line" in
        _* ) line=${var1}$line;;
    esac
    echo $line
done <"file"

output

$ cat file
sldfj
_day 23. 45. 56.
alskjf

$ ./shell.sh
sldfj
this is ok_day 23. 45. 56.
alskjf

if you absolutely want to use sed,

sed -e "/^_/s/^/$var/" file

or with awk

awk -v var="$var" '/^_/{$0=var $0 }1'  file
ghostdog74
thanks for the ideas, but none of them work
asdf
I think I can understand now why your methods do not work for me. When I try with the "this is ok" example, all of them work. But I try something different, now my string comes from: read st1 st2 st3 <<< $( cat ${filespecial} | grep c ) stotal="$st1 $st2 $st3" so it seems that the problem is with stotal: i do not know if ther reason is how I get it (read <<<) or the string concatentation, where i need spaces
asdf
what do you mean none of them works? what error did you get?
ghostdog74
I think I can understand now why your methods do not work for me. When I try with the "this is ok" example, all of them work. But I try something different, now my string comes from: read st1 st2 st3 <<< $( cat ${filespecial} | grep c ) stotal="$st1 $st2 $st3" so it seems that the problem is with stotal: i do not know if ther reason is how I get it (read <<<) or the string concatentation, where i need spaces
asdf
its better to show in your original post what the sample filespecial looks like, what you are getting and show your desired output. If you are looking for lines in filespecial that have "c" (ie grep c) , then you are not getting the correct results into st1,st2 and st3 if there are many lines returned that matched.
ghostdog74
+2  A: 

You don't need to use double quotes around _ and you should not add a global modifier:

sed -e "s/_/${var1}_/" "${filename}"

Whitespaces in $var1 should not be a problem. Whitespaces in $filename can be a problem.

mouviciel
"_" is not added in substittuion
asdf
From your question, you want to replace `_` by `this is ok_`. Did I misunderstand?
mouviciel
I think I can understand now why your methods do not work for me. When I try with the "this is ok" example, all of them work. But I try something different, now my string comes from: read st1 st2 st3 <<< $( cat ${filespecial} | grep c ) stotal="$st1 $st2 $st3" so it seems that the problem is with stotal: i do not know if ther reason is how I get it (read <<<) or the string concatentation, where i need spaces
asdf
A: 

In this case enclose the "this is ok" with single quotes, not double quotes.

as in:

$ var1='this is ok'

Reason being the shell handles whitespace differently depending on the quotation marks used....

Einar Indridason