LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   comma delimited file (https://www.linuxquestions.org/questions/programming-9/comma-delimited-file-23974/)

cdragon 06-21-2002 08:52 AM

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.

crabboy 06-21-2002 09:32 AM

Try posting a 4-5 line example of what the file looks like now and what you need it to look like.

cdragon 06-21-2002 09:51 AM

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.

crabboy 06-21-2002 11:50 AM

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


cdragon 06-21-2002 07:28 PM

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 . . .

cdragon 06-21-2002 07:55 PM

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


All times are GMT -5. The time now is 02:44 PM.