| | |
how to add comma separated fields to a text column?
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
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
--------------------------------------------------------------------------------
to This:
----------------------------------------------------------------------------
FILE 2
----------------------------------------------------------------------------
to This:
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.
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.
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)
ActiveTouchMeetingClient AddressBook Adobe Flash Player Plugin CNXT_MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z Connection Manager DirectDrawEx DXM_Runtime EditPlus 3 ENTERPRISE F95DE19F-CF69-4b03-81B6-9EC050D20D3B
to This:
Shell Scripting Syntax (Toggle Plain Text)
, 1, USERNAME2, ActiveTouchMeetingClient , 1, USERNAME2, AddressBook , 1, USERNAME2, Adobe Flash Player Plugin , 1, USERNAME2, MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z , 1, USERNAME2, Connection Manager , 1, USERNAME2, DirectDrawEx , 1, USERNAME2, DXM_Runtime , 1, USERNAME2, EditPlus 3 , 1, USERNAME2, ENTERPRISE
FILE 2
----------------------------------------------------------------------------
Shell Scripting Syntax (Toggle Plain Text)
ActiveTouchMeetingClient AddressBook Adobe Flash Player Plugin CNXT_MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z Connection Manager DirectDrawEx DXM_Runtime EditPlus 3 ENTERPRISE F95DE19F-CF69-4b03-81B6-9EC050D20D3B
to This:
Shell Scripting Syntax (Toggle Plain Text)
, 2, USERNAME2, ActiveTouchMeetingClient , 2, USERNAME2, AddressBook , 2, USERNAME2, Adobe Flash Player Plugin , 2, USERNAME2, MODEM_PCI_VEN_14F1&DEV_2C06_hpZ1379z , 2, USERNAME2, Connection Manager , 2, USERNAME2, DirectDrawEx , 2, USERNAME2, DXM_Runtime , 2, USERNAME2, EditPlus 3 , 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
#
convertSwToCSVMy 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.
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(){
# 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.
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 ]
dosed -e 's/\(.*\)/, $NextNumber, $Username,\1/'done
$(( NextNumber++ ))
}
# 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.
Sorry about the delayed response. Been working insane hours.
Try
You may want to take a look at this thread: Increment integer using Shell
Try
NextNumber=$(( NextNumber + 1 )) or ((NextNumber++)).You may want to take a look at this thread: Increment integer using Shell
![]() |
Similar Threads
Other Threads in the Shell Scripting Forum
- Previous Thread: How to su properly in a shell script
- Next Thread: trying to gt 1500 pages into a glossary
Views: 1401 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





