Find the answer to your Linux question:
Results 1 to 3 of 3
Hello! I have data I need to sort into a csv file to import into mysql. I can do it, but right now, it looks like making an awk script ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26

    Sorting stuff - awk? sort? other?

    Hello! I have data I need to sort into a csv file to import into mysql. I can do it, but right now, it looks like making an awk script to make an awk script to make an wak script, which I hate. Perhaps one of you kind fellows could offer a more elegant solution?

    [problem] I have 2 files. Each file is fairly similar format wise.
    an example is
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8

    So "customer" is our constant. What I would like is a CSV file that reads
    CUSTOMER info1 info2 ... infoN
    on each line.

    Does this make sense? Any ideas?

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    Once again, probably an inelegant solution, but it works.

    Code:
    chris@angua:~/dev/scratch$ cat test.txt
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info1 info2 CUSTOMER info3 info4
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    info5 CUSTOMER info6 info7 info8
    chris@angua:~/dev/scratch$ sed -n -e 's/\(.* \)\(CUSTOMER \)\(.*\)/\2\1\3/' -e 's/ /,/gp' test.txt
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info1,info2,info3,info4
    CUSTOMER,info5,info6,info7,info8
    CUSTOMER,info5,info6,info7,info8
    CUSTOMER,info5,info6,info7,info8
    CUSTOMER,info5,info6,info7,info8
    CUSTOMER,info5,info6,info7,info8
    CUSTOMER,info5,info6,info7,info8
    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26
    crikey, I completely forgot about this for a while, but the way I solved it in the end was to load it into a MySQL database using awk to seperate them into different tables, and then re-joining them where necessary with SQL.

    Thanks a lot though, that should come in very handy for the future. In fact, it may be just what I need for another problem I have...

Posting Permissions

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