Results 1 to 9 of 9

Thread: sed '/*#/d;/^*$/d' to remove blank lines.

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    Los Angeles, CA
    Beans
    138
    Distro
    Ubuntu 12.04 Precise Pangolin

    sed '/*#/d;/^*$/d' to remove blank lines.

    I've been using vim to do some written homework lately. Yesterday, I encountered a good use for sed.

    Code:
    # Remove comments and blank lines.
    sed '/*#/d;/^*$/d'
    I wanted to test it out on an assignment to remove some blank lines, but I don't think it's working. Let's say my assignment was foo.txt.

    Code:
    sed '/*foo.txt/d;/^*$/d'
    It only results in blank lines themselves until I terminate it. How can I make this work?
    Last edited by epikvision; May 11th, 2013 at 04:48 AM.
    John J. Kim (IRC: kotux)
    High School Senior / Ubuntu User # 35405
    www.launchpad.net/~kotux
    "Let your performance do the thinking." --Charlotte Bronte


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

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    foo.txt is a file you want to process?
    Code:
    sed 'your expression here' foo.txt
    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

  3. #3
    Join Date
    Jun 2006
    Location
    Brisbane Australia
    Beans
    713

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    You want to remove comments and blank lines?
    Code:
    sed -e'/^#/d' -e'/^$/d' foo.txt

  4. #4
    Join Date
    Feb 2012
    Location
    Los Angeles, CA
    Beans
    138
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    Can you explain what /^#/d means? I get that ^$ from start to the end of a line.
    John J. Kim (IRC: kotux)
    High School Senior / Ubuntu User # 35405
    www.launchpad.net/~kotux
    "Let your performance do the thinking." --Charlotte Bronte


  5. #5
    Join Date
    Jun 2006
    Location
    Brisbane Australia
    Beans
    713

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    Quote Originally Posted by epikvision View Post
    Can you explain what /^#/d means? I get that ^$ from start to the end of a line.
    Delete any line starting with a "#', i.e. a comment line.

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

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    ^ and $ are start-of-line and end-of-line markers, ^$ simply means 'nothing between start and end (empty line)', while ^# means '# right at the beginning of the line'.
    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

  7. #7
    Join Date
    Feb 2012
    Location
    Los Angeles, CA
    Beans
    138
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    Is there a special use for this command? I assume it removes all bells and whistles and shows only raw code without any formatting to look at.
    John J. Kim (IRC: kotux)
    High School Senior / Ubuntu User # 35405
    www.launchpad.net/~kotux
    "Let your performance do the thinking." --Charlotte Bronte


  8. #8
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    Hi

    Personally i use this as it also strips off # comments in the middle of a line and strips spaces off the end of a line.

    Code:
    sed -e 's/#.*$//' -e 's/[ ]*$//' -e '/^$/d'
    Code:
    matthew-S206:/home/matthew/useful_commands_dir % cat tmp
    This
    #deb http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu raring main
    is
    
    
    #deb http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu raring main
    a test #        test
    matthew-S206:/home/matthew/useful_commands_dir % sed -e 's/#.*$//' -e 's/[ ]*$//' -e '/^$/d' tmp
    This
    is
    a test
    matthew-S206:/home/matthew/useful_commands_dir %
    EDIT: You can use a semi colon as a delimiter as well.

    Kind regards
    Last edited by matt_symes; May 2nd, 2013 at 06:30 PM.
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  9. #9
    Join Date
    Feb 2012
    Location
    Los Angeles, CA
    Beans
    138
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed '/*#/d;/^*$/d' to remove blank lines.

    Wow, it makes things seem pretty darn clean! Thanks for all the posts.
    John J. Kim (IRC: kotux)
    High School Senior / Ubuntu User # 35405
    www.launchpad.net/~kotux
    "Let your performance do the thinking." --Charlotte Bronte


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
  •