Results 1 to 3 of 3

Thread: Inserting newlines into text file using tr or sed or awk

  1. #1
    Join Date
    Sep 2008
    Location
    Asheville, NC, US
    Beans
    65
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Inserting newlines into text file using tr or sed or awk

    I've got a rather large text file, specifically a hosts file, that isn't formatted properly. It's all one line. I need to put a newline before every occurence of 127.0.0.1. I can't find a way to do it easily in Kate or LibreOffice, and I don't really understand enough about sed or awk to be able to figure it out. I tried using tr, but got varied results, some close, but none right. Most recently, I tried the example below, but it took out all the "127.0.0", and put "1" in a lot of places I didn't want it.

    Code:
    cat hosts | tr "127.0.0.1 " "\n127.0.0.1 " > newHosts
    I'm new to this aspect of Linux, and I know I have a lot of reading to do to really understand tr and sed and awk, but can someone help me with this particular example?

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Inserting newlines into text file using tr or sed or awk

    Code:
    sed 's/127.0.0.1/\n&/g'
    maybe? 'tr' only translates a single character at a time, I think

  3. #3
    Join Date
    Sep 2008
    Location
    Asheville, NC, US
    Beans
    65
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Inserting newlines into text file using tr or sed or awk

    Quote Originally Posted by steeldriver View Post
    Code:
    sed 's/127.0.0.1/\n&/g'
    maybe? 'tr' only translates a single character at a time, I think
    That did it! Thanks so much for the help.

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
  •