Cris
Cris

Reputation: 796

Use sed command along with readlink (or simmilar) to replace full path into text

I'm writing a BASH script in which I want to use sed to replace the fullpath of a file into a text.

I currently have this:

Path= readlink -e /home/user/Videos/*.mp4
sed -i "s/original_text/$Path/g" /home/user/Documents/Text.txt

The idea is to replace the path of a single MP4 video (there aren't other videos in that folder) into a text file so I don't have to write the name of the video every time I want to replace it.

I have tried different options for this problem but I can't make it work.

Thanks in advance.

Upvotes: 0

Views: 717

Answers (2)

Kent
Kent

Reputation: 195209

I believe that your $Path has slash(/) in value, e.g. /home/user/Videos/foo.mp4, this will make your sed fail. You can use other delimiter for sed's s command.

Try this sed line:

sed -i "s@original_text@$Path@g" /home/user/Documents/Text.txt

also you should quote your readlink command, and remove the space right after =

Upvotes: 1

heemayl
heemayl

Reputation: 42107

Use command substitution to save the output of a command in a variable:

Path="$(readlink -e /home/user/Videos/*.mp4)"

Also the shell does not support any space(s) around = while declaring variables.


If you have multiple files, better use an array:

Paths=( $(readlink -e /home/user/Videos/*.mp4) )

and access the array elements by "${Paths[@]}"

Upvotes: 1

Related Questions