LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-21-2002, 08:52 AM   #1
cdragon
LQ Newbie
 
Registered: Jun 2002
Posts: 4

Rep: Reputation: 0
Question comma delimited file


I have a comma delimited file that I want to get into a database. The file contains three lines of data for each record. The database program I'm using reads each line as a separate record, so I can't get the DB program to read the file properly.

I need a way to get the three lines onto one line so the DB program can read it. There are far too many records to do this by hand. From looking at some things, I would think there must be an easy way to do this with something like perl. Any suggestions?

I have little experience with perl but I am learning, and I am willing to learn something else if perl isn't the right way to go. I know some people post on here hoping someone will just give them the solution. If someone wants to do that, that's fine with me (it would certainly simplify things for me). But I hope to at least get a direction to go in on this issue. Thanks in advance.
 
Old 06-21-2002, 09:32 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Try posting a 4-5 line example of what the file looks like now and what you need it to look like.
 
Old 06-21-2002, 09:51 AM   #3
cdragon
LQ Newbie
 
Registered: Jun 2002
Posts: 4

Original Poster
Rep: Reputation: 0
example

It looks like this:

1, a, b, c
1, d, e, f, g
1, h, i, j, k
1, l, m, n, o
2, a, b, c
2, etc.

I need it to look like this:

1, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o
2, a, b, c, etc.
3, etc.

It would be okay if the "1"s were in there as well.
Thanks.
 
Old 06-21-2002, 11:50 AM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I'm not sure from your example which way you wanted it. Your first post stated every three lines. Your example showed that the lines were indexed by a number and you should join all the numbers. So I tried it both ways. The first example will split by indexed lines, the second will join every three lines.

Let me know if this works for you.

Change the 'datafile' to be whatever the name is of your datafile.
Code:
#!/bin/sh

CURRENT_STR=""
LAST_NUM=0

for i in `cat datafile`; do
  CURRENT_NUM=`echo $i | cut -d',' -f1`
  if [ $CURRENT_NUM -eq $LAST_NUM ]; then
     CURRENT_STR="$CURRENT_STR,$i"
  else
     echo $CURRENT_STR
     CURRENT_STR=$i
     LAST_NUM=$CURRENT_NUM
  fi

done
echo $CURRENT_STR
Run this example like: cat datafile | parse.sh
Code:
#!/bin/sh

COUNT=0
CURRENT_STR=""

NEXT=""
while [ 1 ]; do
   COUNT=0
   while [ $COUNT -lt 3 ]; do
      read -t1 NEXT
      if [ "x$NEXT" = "x" ]; then  
         exit;
      fi

      if [ "x$CURRENT_STR" = "x" ]; then
         CURRENT_STR=$NEXT
      else
         CURRENT_STR="$CURRENT_STR,$NEXT"
      fi
   COUNT=`expr $COUNT + 1`
   done
   echo $CURRENT_STR
   CURRENT_STR=""

done
 
Old 06-21-2002, 07:28 PM   #5
cdragon
LQ Newbie
 
Registered: Jun 2002
Posts: 4

Original Poster
Rep: Reputation: 0
Okay, the second one is doing the job nicely. Thank you very much.

One last thing, and I feel like I'm missing something simple here, how do I get the output into a new file? It seems to just be showing it to me and not altering the file or creating a new one.

I need a good shake to get the neurons working . . .
 
Old 06-21-2002, 07:55 PM   #6
cdragon
LQ Newbie
 
Registered: Jun 2002
Posts: 4

Original Poster
Rep: Reputation: 0
Smile

*shakes head*
Okay, neurons working now. Output going into file. Thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 08:42 AM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 09:05 AM
Parsing a tab delimited text file jajanes Programming 9 08-08-2003 10:34 AM


All times are GMT -5. The time now is 10:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration