1

I try to use sed to change a line in a file named convergence.gnu I have a variable named lafila, which is a file name

Currently, I can do:

lafila="nGas060.dat"
sed -i "6s/.*/plot \"$lafila\" using 1:2 with l/" convergence.gnu

This changes correctly the sixth line of my convergence.gnu file to:

plot "nGas062.dat" using 1:2 with l

However, now I want to include a dollar sign in the replaced line to obtain instead:

plot "nGas062.dat" using ($1/1000):2 with l

Can you tell me what to change in my sed command? If I escape a dollar sign it does not work properly. Double dollars also don't work.

Nando
  • 170
  • 3
  • 9
  • could you please add sample Input and expected output too? It will be good for all to understand the question. – RavinderSingh13 Oct 20 '17 at 04:56
  • Escaping the dollar sign with a backslash does work (`$ echo "\$PATH" # prints $PATH`), so can you show us the exact command you tried and the result? – Kevin Oct 20 '17 at 05:04
  • OK, I edited to make clear that in the sed I want to print a variable replacement first and then I want to print the dollar sign. – Nando Oct 20 '17 at 05:38
  • You can use the sed's `c` command to replace a line with specified text, which avoids having to worry about `/` characters in the replacement text. – rici Oct 20 '17 at 06:05

3 Answers3

1

I believe your issue is actually being caused by the forward slash in ($1/1000), which clashes with the slashes being used to delimit the various components of the sed command. You either need to escape that forward slash as well, or alternatively use a different character for delimiting the sed strings. Either of the below should work:

lafila="nGas060.dat"
sed -i "6s/.*/plot \"$lafila\" using (\$1\/1000):2 with l/" convergence.gnu

or

lafila="nGas060.dat"
sed -i "6s,.*,plot \"$lafila\" using (\$1/1000):2 with l," convergence.gnu

Using a different delimiting character can be a good way to make your sed string look neater and avoid the leaning toothpick syndrome.

echo foo | sed "s,foo,/there/are/a/lot/of/slashes/here,"

is much nicer than

echo foo | sed "s/foo/\/there\/are\/a\/lot\/of\/slashes\/here/"
  • None of the commands given could read the `lafila` variable. First and second one resulted in: `plot "" using ($1/1000):2 with l` with empty space where `lafila` variable should be – Nando Oct 20 '17 at 05:34
  • That's just because I left out of the part where `lafila` was defined as I didn't think you had an issue with that section. I was just addressing the sed command that seemed to be giving you a problem. I'll edit my answer. – fractal_sounds Oct 20 '17 at 05:39
0

Use single quotes:

sed -i '6s/.*/plot "'$lafila'" using ($1\/1000):2 with l/' convergence.gnu

Single quotes protect double quotes and $ is not interpreted inside them. However, you do need to escape /.

See also:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • But nGas062.dat is a variable named `$lafila`. First I need the sed to replace $lafila and then to use the $1/1000 That is, if variable `$lafila=nGas062.dat` Then I need the output: `plot "nGas062.dat" using ($1/1000):2 with l` – Nando Oct 20 '17 at 05:25
  • Updated the answer. – codeforester Oct 20 '17 at 05:28
  • 1
    **Perfect.** That one produced the expected result. Thanks!: `sed -i '6s/.*/plot "'$lafilas'" using ($1\/1000):2 with l/' convergence.gnu` Resulted in: `plot "nGas060.dat" using ($1/1000):2 with l` – Nando Oct 20 '17 at 05:43
0

Try it:

lafila="nGas060.dat"
sed -i "6s@.*@plot \"$lafila\" using (\\\$1/1000):2 with l@" convergence.gnu

You need only to escape backslash and after the dollar sign.

\\ + \$ == \\\$
Darby_Crash
  • 446
  • 3
  • 6