Find the answer to your Linux question:
Results 1 to 8 of 8
Hi, I'm quite new to Linux and Linux scripting and I would like to know how to deal with the following task: I have a text file which consist one ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    3

    One long line to many separate lines

    Hi,
    I'm quite new to Linux and Linux scripting and I would like to know how to deal with the following task:

    I have a text file which consist one long row. Row is basically in xml syntax and may have lot of fields. Source example:

    <data1>1a</data1><data2>2b</data2><data3>3c</data3>…<dataN>Nn</dataN>

    Now I should populate the file so that every field in that long line goes into separate row. Result example:

    <data1>1a</data1>
    <data2>2b</data2>
    <data3>3c</data3>

    <dataN>Nn</dataN>


    What might be the best way to receive the result above?

    Thanks,
    --
    Don

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    hi,


    with this you should be OK
    Code:
    sed -i 's/></>\n</g' file_name
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    3
    Hi,
    Thanks khafa! Today I work with Windows and I downloaded sed -software for Win. I run sed to make simple changes to example file and there were ok. For example code below worked perfectly:

    sed -i "s/1a/AAA/g" file_name.txt

    The "1a" was replaced with "AAA".

    Then I had some problems with the code your kindly sent (little modified):

    sed -i "s/></AAA/g" file_name.txt

    When I run the code from above in command prompt it didn't replace "><" with "AAA". I tried some of this and some of that, but without success. Should "><" -chars in the pattern be used with some special characters?

    Thanks again!

    --
    Don

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Quote Originally Posted by donstevens23 View Post
    When I run the code from above in command prompt it didn't replace "><" with "AAA". I tried some of this and some of that, but without success. Should "><" -chars in the pattern be used with some special characters?

    Thanks again!

    --
    Don
    hi,


    I tested the above code with my Linux box before sending it. it works fine


    Edit- notice that i used single quotation in my code while you are using double quotation in yours.
    Linux and me it's a love story

  5. #5
    Just Joined!
    Join Date
    Jun 2008
    Posts
    3
    Hi,
    Sorry, my mistake. Khafa's code works like it should.


    --
    Don

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    dont worry. welcome to the wonderful Linux world
    Linux and me it's a love story

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    it might be better to consider situations like this: (spaces in between)
    eg
    <data1>1a</data1> <data2>2b</data2> <data3>3c</data3>…<dataN>Nn</dataN>
    Code:
    sed  's/> *</>\n</g' file

  8. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by khafa View Post
    Edit- notice that i used single quotation in my code while you are using double quotation in yours.
    Doesn't make any difference with the patterns you're using.

Posting Permissions

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