Find the answer to your Linux question:
Results 1 to 3 of 3
I have a large text file over 40k lines in the format: Some Company 999 N Oak Trfy, Kansas City, MO, 64118 And would like to know of a way ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2005
    Posts
    1

    Suggestions for converting a text file format.


    I have a large text file over 40k lines in the format:

    Some Company
    999 N Oak Trfy,
    Kansas City,
    MO,
    64118

    And would like to know of a way to change it to:


    Some Company, 999 N Oak Trfy, Kansas City, MO, 64118


    Any suggestions would be greatly appreciated.

  2. #2
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Use the tr command in bash.


    Code:
    tr '\n' ' ' < /path/to/file > /path/to/newfile
    edit:
    This will make all companies appear on the same line, but assuming you have a line break between companies, you can just run another script after the first one, which will replace double spaces with a new line.

    Code:
    tr '  ' '\n' < /path/to/newfile > /path/to/newfile2
    Last edited by Cancerous; 09-26-2012 at 12:24 AM.

  3. #3
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by Cancerous View Post
    Code:
    tr '  ' '\n' < /path/to/newfile > /path/to/newfile2
    That will replace all spaces with a newline! tr works on sets of characters (try it for yourself: "echo ab | tr ab A"). To replace more than one character, you'd need sed. Following the suggestion above:

    Code:
    sed 's.  .\n.g' oldfile > newfile
    However, if there isn't an extra line break between each record, you'll probably need a (quick) custom program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •