943,910 Members | Top Members by Rank

Ad:
Mar 12th, 2009
0

how to add comma separated fields to a text column?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
siul0_0 is offline Offline
3 posts
since Mar 2009
Mar 25th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
mittelgeek is offline Offline
37 posts
since Aug 2005
Mar 25th, 2009
0

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

HI There!

Thanks for the response.

I am getting a syntax error on the $(( NextNumber++ )).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
siul0_0 is offline Offline
3 posts
since Mar 2009
Apr 23rd, 2009
0

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

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

Click to Expand / Collapse  Quote originally posted by siul0_0 ...
HI There!

Thanks for the response.

I am getting a syntax error on the $(( NextNumber++ )).
Reputation Points: 10
Solved Threads: 0
Light Poster
mittelgeek is offline Offline
37 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: 1500 static html pages into mediawiki format
Next Thread in Shell Scripting Forum Timeline: trying to gt 1500 pages into a glossary





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC