Meghraj Suthar
Meghraj Suthar

Reputation: 43

How to Append a string after some specific characker in Shell

I have a script to download video from youtube but i want to add ss just after the www. to it will be converted into www.ssyoutube.com

 #!/bin/bash
 dialog --inputbox "Enter Video's Link..." 10 30 2>/tmp/video.txt
 video=`cat /tmp/video.txt`       
 edit=ss 
 echo $video
 sleep 5;
 wget $video

Upvotes: 0

Views: 92

Answers (2)

tomclegg
tomclegg

Reputation: 485

Parameter Expansion can do this for you in bash.

video="www.youtube.com"
edit="ss"
video="${video/www./www.$edit}"
echo "$video"                   # www.ssyoutube.com

man bash → search for "Pattern substitution."

(Sure, sed works too, but for a simple string substitution it's much more efficient to use bash's built-in feature than to fork a new process.)

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

You could use sed. Use a escaped dot in your regex to match a literal dot.

sed 's/www\./www.ss/g' file

ie,

sed -i 's/www\./www.ss/g' /tmp/video.txt

Upvotes: 0

Related Questions