ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
for i in `cat datafile`; do
CURRENT_NUM=`echo $i | cut -d',' -f1`
if [ $CURRENT_NUM -eq $LAST_NUM ]; then
Run this example like: cat datafile | parse.sh
while [ 1 ]; do
while [ $COUNT -lt 3 ]; do
read -t1 NEXT
if [ "x$NEXT" = "x" ]; then
if [ "x$CURRENT_STR" = "x" ]; then
COUNT=`expr $COUNT + 1`