-1

I am completely new to sed script. I want to make a script so that it asks user to enter value to be inserted in file. I am not able to find the correct code

so the line I have in the text file looks like this

path =

I want to input path from user so the line looks like:

path = /dev

Through trial and error I only have:

read -p "Enter path name: " PT
echo "$( sed -i '/path/ s/.*/& $PT/' textfile )"

which gives

path = $PT

can you help me with correct code

  • What is "sed script"? Are you new to the command sed or are you new to scripting? If the latter please indicate what shell you are using. – Anthon Sep 18 '17 at 16:19

2 Answers2

0
read -p "Enter path name: " PT
sed -i "/path/ s#.*#& $PT#" textfile

You need to use double quotes, not single quotes. Single quotes protects a string from the shell, but in this case you want the shell to expand the value of $PT.

Also,

echo "$( ... )"

is the same as just ... (the stuff in the $( ... )).

Kusalananda
  • 333,661
0

Variable expansion doesn't happen within single quote, use double quote instead.

+ since your variable will contains / as part of path. with GNU sed you should use different substitution delimiter.

read -p "Enter path name: " PT
sed -i "\#path# s#.*#& $PT#" infile
αғsнιη
  • 41,407