how to add comma separated fields to a text column?

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 3
Reputation: siul0_0 is an unknown quantity at this point 
Solved Threads: 0
siul0_0 siul0_0 is offline Offline
Newbie Poster

how to add comma separated fields to a text column?

 
0
  #1
Mar 12th, 2009
Hi there!!

My first Post

My problem is as follows:

I have multiple text files in a folder, and I need to automatically format these text files to an CSV format. At the same time I need to add different Data to accommodate it to the MySQL database.

so for example I need to convert this:
--------------------------------------------------------------------------------
FILE 1
--------------------------------------------------------------------------------
Shell Scripting Syntax (Toggle Plain Text)
  1. ActiveTouchMeetingClient
  2. AddressBook
  3. Adobe Flash Player Plugin
  4. CNXT_MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z
  5. Connection Manager
  6. DirectDrawEx
  7. DXM_Runtime
  8. EditPlus 3
  9. ENTERPRISE
  10. F95DE19F-CF69-4b03-81B6-9EC050D20D3B

to This:

Shell Scripting Syntax (Toggle Plain Text)
  1. , 1, USERNAME2, ActiveTouchMeetingClient
  2. , 1, USERNAME2, AddressBook
  3. , 1, USERNAME2, Adobe Flash Player Plugin
  4. , 1, USERNAME2, MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z
  5. , 1, USERNAME2, Connection Manager
  6. , 1, USERNAME2, DirectDrawEx
  7. , 1, USERNAME2, DXM_Runtime
  8. , 1, USERNAME2, EditPlus 3
  9. , 1, USERNAME2, ENTERPRISE
----------------------------------------------------------------------------
FILE 2
----------------------------------------------------------------------------
Shell Scripting Syntax (Toggle Plain Text)
  1. ActiveTouchMeetingClient
  2. AddressBook
  3. Adobe Flash Player Plugin
  4. CNXT_MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z
  5. Connection Manager
  6. DirectDrawEx
  7. DXM_Runtime
  8. EditPlus 3
  9. ENTERPRISE
  10. F95DE19F-CF69-4b03-81B6-9EC050D20D3B

to This:

Shell Scripting Syntax (Toggle Plain Text)
  1. , 2, USERNAME2, ActiveTouchMeetingClient
  2. , 2, USERNAME2, AddressBook
  3. , 2, USERNAME2, Adobe Flash Player Plugin
  4. , 2, USERNAME2, MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z
  5. , 2, USERNAME2, Connection Manager
  6. , 2, USERNAME2, DirectDrawEx
  7. , 2, USERNAME2, DXM_Runtime
  8. , 2, USERNAME2, EditPlus 3
  9. , 2, USERNAME2, ENTERPRISE

Etc.. Etc.. up to 500 files doesn't matter.

As you can see the Second column needs to increment +1 on each file it loops through.

Maybe I took the wrong approach to this, and hopefully there's someone that's going to point me in the right direction.

Here's what I've got so far.

convertSwToCSV(){
    cd $PATHTOBUILD
    COUNT=60 # THIS NEEDS TO BE THE LINE COUNT OF THE FILES INSIDE THE LOOP
   for ((a=1; a <= $COUNT; a++))
   do
        for txtFiles in *; do
        USERNAME=`cat $PATHTOHARDWARE$txtFiles | grep Logged | awk -F '\'  '{print $2}'`
    echo $USERNAME #just to see if we are getting the correct info.
        cat $txtFiles | awk '{sub(/^/, " , '${a}', '${USERNAME}', ");print}' > $txtFiles
        done
   done
}

####################
# Runtime starts here
#
convertSwToCSV


My Idea is to do a FOR LOOP to create the auto_increment numbers between the file. BUT I am at LOST on how to end the LOOP inside the for txtFiles in *; do.

Any help on this will be GREATLY appreciated.

Thanks guys, sorry for the big post.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: mittelgeek is an unknown quantity at this point 
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: how to add comma separated fields to a text column?

 
0
  #2
Mar 25th, 2009
I think you should take a quick look at this thread: Adding a new column in a text file. It seems that they are requesting something very similar to what you would like to do.

As for what you have now, you can use sed:

convertSwToCSV(){
cd $PATHTOBUILD

NUMBER_OF_FILES=Put total number of files to be modified here

NextNumber=0
Username=`grep Logged '$PATHTOHARDWARE$txtFiles' | awk -F '\' '{print $2}'`

while [ $NextNumber -lt $NUMBER_OF_FILES ]
do
sed -e 's/\(.*\)/, $NextNumber, $Username,\1/'
$(( NextNumber++ ))
done
}
####################
# Runtime starts here
#
convertSwToCSV

And I think you will end up with the same result. You may want to double check some of my constructs before putting it into production use.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: siul0_0 is an unknown quantity at this point 
Solved Threads: 0
siul0_0 siul0_0 is offline Offline
Newbie Poster

Re: how to add comma separated fields to a text column?

 
0
  #3
Mar 25th, 2009
HI There!

Thanks for the response.

I am getting a syntax error on the $(( NextNumber++ )).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: mittelgeek is an unknown quantity at this point 
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: how to add comma separated fields to a text column?

 
0
  #4
Apr 23rd, 2009
Sorry about the delayed response. Been working insane hours.

Try NextNumber=$(( NextNumber + 1 )) or ((NextNumber++)).

You may want to take a look at this thread: Increment integer using Shell

Originally Posted by siul0_0 View Post
HI There!

Thanks for the response.

I am getting a syntax error on the $(( NextNumber++ )).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1386 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC