Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Another sed question

  1. #1
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Another sed question

    Hi Guys,

    I've been playing with sed and have something I need some help understanding.

    I have some text that looks like this:

    Code:
    [ ] Turn on rice cooker @ 16:30
    I would like to put a ; after the ].
    At first I thought I could use:

    Code:
    sed -e 's/[ ]/[ ];/'
    But I ended up with:

    Code:
    [[ ];] Turn on rice cooker @ 16:30
    I tried:
    Code:
    sed -e 's/[]/[];/'
    and got an error message that
    Code:
    sed: -e expression #1, char 9: unterminated `s' command
    What is the best way to add the semi colon?
    Last edited by GrouchyGaijin; May 17th, 2013 at 10:11 AM.
    Thank you,
    GG -----------

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    escape

    You have to escape the square brackets in the first part of the regex to get them treated as individual characters. Or else sed will think you are looking for the set of characters listed between them.

    Code:
    sed -e 's/\[ \]/[ ];/'
    Also, mind the space(s).

  3. #3
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Another sed question

    Thank you Lars!
    Thank you,
    GG -----------

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Another sed question

    You're welcome. If you want to see more of what square brackets are and how they work when not escaped check out this link:

    http://www.regular-expressions.info/posixbrackets.html

  5. #5
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Another sed question

    Hi Lars (or anyone passing by),

    I have another question for you.

    I run a script that uploads video to YouTube. The script returns the url for the video.

    I have everything before the part I need stripped out with a sed command and then I want to add:

    Code:
    {youtube}important_part_of_the_url?rel=0{/youtube}
    I've almost got it.

    I've set the command as:
    Code:
    test.txt | sed 's/^.*=//'| sed 's/$/?rel=0/'| sed 's/^/{youtube} /' | sed 's/$/{\/\youtube}/' >
    The thing is, that puts a space after the {youtube} and before the url starts.

    Example:
    Code:
    {youtube} eRXvk7cb3VE?rel=0{/youtube}
    Do you know how I can get rid of that space?
    Thank you,
    GG -----------

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Another sed question

    I'm not clear on what the sample input would be and the desired output. But you can write the sed lines in two other ways:

    Code:
    # this
    cat test.txt | sed 's/^.*=//' -e 's/$/?rel=0/' -e 's/^/{youtube} /' -e  's/$/{\/\youtube}/' 
    
    # or this 
    cat test.txt | sed 's/^.*=//; s/$/?rel=0/; s/^/{youtube} /; s/$/{\/\youtube}/'
    As for the space, it looks like you are adding it on purpose. Check the boldfaced part of the second example above for the location. Try removing the trailing space.

  7. #7
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Another sed question

    That's because you have a space in your repl string:
    Code:
    sed 's/^/{youtube} /'
                      ^-- right here
    You can do multiple sed commands in a single line by separating them with ; -- no need for four pipes or a cat.
    Code:
    sed -e 's/^.*=//; s/$/?rel=0/; s/^/{youtube}/; s/$/{\/\youtube}/' test.txt
    However, you can also do this in a single replacement:
    Code:
    sed -e 's#^.*=\(.*\)#{youtube}\1?rel=0{/youtube}#' test.txt
    which will turn a file full of https://www.youtube.com/watch?v=2IHxFRROHCA into a list of {youtube}2IHxFRROHCA?rel=0{/youtube} (which is what I assume you're going for).

    (ninja edit -- too slow)

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Another sed question

    drop ?rel=0 too, it's junk. video id is that 11 char long string, you can take advantage of that. Probably you can extract it easily with grep
    Code:
    grep -oP '[A-Za-z0-9_-]{11}'
    Last edited by Vaphell; May 19th, 2013 at 08:32 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Another sed question

    Thanks Guys,

    I didn't realize I had put that space in.
    The ?rel=0 is so that when someone is done watching a particular video I've embedded on the website they don't see a bunch of other "related" videos as suggestions. I think that with just the 11 character string, those suggestions pop up at the end of a clip.
    Thank you,
    GG -----------

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Another sed question

    probably but you still don't need to store that rel, in a file with a bunch of video ids it's nothing more than a redundant information and a waste of space.
    Code:
    videoid1&rel=0
    videoid2&rel=0
    videoid3&rel=0
    ...
    Add it only when you need it, loop that will do that is trivial, eg
    Code:
    while read v_id
    do
      # generate html page links here or whatever
      url="http://youtube/${v_id}?rel=0"
      echo $url
    done < video_ids.txt
    Last edited by Vaphell; May 19th, 2013 at 08:47 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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