Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    May 2012
    Posts
    85

    replace substring in lines using sed or grep


    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 is always 11 characters/numbers

    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
    Code:
    sed -i 's/\(videoId: \).*\\1'${id}'/\2/' file
    but there are errors, what is wrong with my script?

    thanks

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    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;
    the substitution is on the right hand side. the pattern is on the lhs.

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    20
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •