Results 1 to 3 of 3

Thread: Removing the blank lines in `fortune'

  1. #1
    Join Date
    Jul 2009
    Beans
    68

    Removing the blank lines in `fortune'

    Okay, hi. When I execute a fortune, it puts out into many lines into IRC, despite the length and screensize.

    I run the command

    /exec -o fortune -s | sed 's/\t//g' | sed 's/\n//g'

    That SHOULD remove all the newlines, that cause my fortune to look like this, right?
    21:12:36 You can't erase a dream, you can only wake me up.
    21:12:36 -- Peter Frampton
    See, even though I remove the newline character, \n, it still makes 2 or more lines. (the removal of \t is so I do not get an error in some rooms).



    So help me exec fortune all into 1 message please! I have tried everything to fix it.


    Thanks in advance.

  2. #2
    Join Date
    Jul 2009
    Beans
    68

    Re: Removing the blank lines in `fortune'

    Oh awesome. echo == awesome.

    /exec -o echo `fortune -s` | sed 's/\t//g' | sed 's/\n//g'

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Removing the blank lines in `fortune'

    and just to explain why your sed approach won't work ... its not possible to strip newlines using sed like that, since sed operates on one line at a time, there isn't actually any newline character in each line for it to replace.

    you can make sed do what you want in a slightly more convoluted way:
    Code:
    sed ':a;N;$!ba;s/\n//g' somefile
    :a create a label to branch back to later
    N read in the next line and add it to the pattern space
    $!ba for every line except the last, branch back to the start of our expression (ie. keep on reading in subsequent lines)
    s/\n//g on the last line, remove all newlines from pattern space (and print the result)

    Code:
    cat somefile | tr -d '\n'
    is almost an equivalent, except that you don't get a final newline this way

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
  •