Results 1 to 7 of 7

Thread: Sed/Awk/Whatever - Remove SOME Line Breaks

  1. #1
    Join Date
    Aug 2008
    Beans
    14

    Sed/Awk/Whatever - Remove SOME Line Breaks

    This seemed like as good a place as any to ask this question. Let's say you have a text file that contains the following:

    Code:
    Hello
    World
    Hello 
    World
    Hello 
    World
    What is one way that you can turn this into:

    Code:
    Hello World
    Hello World
    Hello World
    Not all line breaks are removed. Only the ones that are between "Hello" and "World" are replaced by spaces. Any ideans?

  2. #2
    Join Date
    Oct 2007
    Beans
    1,820

    Re: Sed/Awk/Whatever - Remove SOME Line Breaks

    Maybe this problem is more complicated, but this sounds like a job for your text editor, not for sed and awk. in nano, or emacs will come with search and replace.

    If you explain your actual problem, we can probably code something up.

  3. #3
    Join Date
    Oct 2006
    Location
    Lyon, France
    Beans
    839
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Sed/Awk/Whatever - Remove SOME Line Breaks

    A perl one-liner does the job.

    Cute one:

    Code:
    perl -ne 'chomp and print "$_ " and next if /Hello/; print' filename.txt
    Practical one:

    Code:
    perl -ne 's/\n/ / if /Hello/; print' filename.txt
    Replace /Hello/ with a regular expression that suits you better if necessary.
    This is the first age that's paid much attention to the future, which is a little ironic since we may not have one.
    -- Arthur C. Clarke

  4. #4
    Join Date
    Aug 2008
    Beans
    14

    Re: Sed/Awk/Whatever - Remove SOME Line Breaks

    Sorry if this sounds ignorant, but I typically use vi for my console text editing and am not overly comfortable with nano or emacs. I could probably learn how to do a simple search and replace with one of them, but this seems to be a reoccurring issue in some of my scripting and I don't always have the ability to edit the text file in question.

    Basically, like my example, I have a text file where I have repeated occurrences of a line break getting in the way of manipulating a string of text. In the case of what I'm doing now, I have addresses listed as:
    Code:
    123 Main St.
    Your Town, USA 12345
    I would prefer to store this as:
    Code:
    123 Main St. Your Town, USA 12345
    All other line breaks in the file are not a problem, just the one that breaks the address apart.

  5. #5
    Join Date
    Aug 2008
    Beans
    14

    Re: Sed/Awk/Whatever - Remove SOME Line Breaks

    Thank you Mornedhel. I'll give that a try.

  6. #6
    Join Date
    Aug 2008
    Beans
    14

    Re: Sed/Awk/Whatever - Remove SOME Line Breaks

    Works like a champ! Thanks again.

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

    Re: Sed/Awk/Whatever - Remove SOME Line Breaks

    just to add a sed equivalent:
    Code:
    sed '/Hello/N;s/\n/ /' somefile
    ...match lines containing Hello, for each of these, read in the following line and replace the newline between the 2 with a space.

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
  •