+ Reply to Thread
Results 1 to 3 of 3

Thread: Using sed

  1. #1
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    92
    Rep Power
    2

    Using sed

    I have a huge list of phone numbers, each on its own line, in a single 130+MB text file.

    I need to append (' to the beginning of each line and '), to the end.

    sed, for whatever reason, doesn't like when I try:
    Code:
    sed -i 's/^/\(\'/' txt
    
    it gives me a continuation prompt. After I enter a closing ' character, I get the error:
    sed: -e expression #1, char 12: unterminated `s' command
    However, if I try:
    Code:
    sed -i 's/^/\(/' txt
    
    it works as expected. For whatever reason, I'm not allowed to escape the ' character in the first example.

    Can anyone tell me what I'm doing wrong?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,354
    Blog Entries
    9
    Rep Power
    19

    Shouldn't it be like this?
    Code:
    sed -i 's/^/\(\'' txt
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    92
    Rep Power
    2

    Murray: That still give me the continuation prompt and, when I provide a closing ', throws the unterminated s error.

    However, I did get a solution that worked. I also found a problem I was unaware of.

    The solution was to enclose the s/^/... with double-quotes like thus:
    Code:
    sed -i "s/^/\('/" txt
    
    Which, in retrospect, makes sense.

    The problem I found (that I had been tearing my hair out over) was with the text file itself. For some reason, it was DOS formatted and when I tried a sed command using $ to indicate the end of the line, the replacement text would appear on the line below. After realizing this error, I used dos2unix on the file and was able to proceed without additional error.

+ Reply to 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

SEO by vBSEO