Results 1 to 3 of 3
I have a file with a lot of lines, two of them are:
Code:
videoId: 'S2Rgr6yuuXQ'
var vid_seq=1;
in a shell script, I have two variables, for id, the value ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-08-2012 #1Just Joined!
- Join Date
- May 2012
- Posts
- 82
replace substring in lines using sed or grep
I have a file with a lot of lines, two of them are:
in a shell script, I have two variables, for id, the value is always 11 characters/numbersCode:videoId: 'S2Rgr6yuuXQ' var vid_seq=1;
id='fsafsferii2'
id_seq=80
I want to modify these two lines with id and id_seq
videoId: 'fsafsferii2'
var vid_seq=80;
I used
but there are errors, what is wrong with my script?Code:sed -i 's/\(videoId: \).*\\1'${id}'/\2/' file
thanks
- 11-08-2012 #2Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
hi,
the substitution is on the right hand side. the pattern is on the lhs.Code:$ cat input.file videoId: 'S2Rgr6yuuXQ' var vid_seq=1; $ id='fsafsferii2' $ sed "s/\(videoId: '\)[^']*/\1$id/" esolve.in.lf videoId: 'fsafsferii2' var vid_seq=1;
- 11-09-2012 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 18
To make a very precise conditional replacement, here is my proposal:
Code:id='fsafsferii2' id_seq=80 sed -ri "s/^(videoId: )'[[:alnum:]]{11}'/\1'${id}'$/;s/^(var vid_seq)=[0-9]+$/\1=${id_seq}'/" file


Reply With Quote
