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

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:

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

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:

, 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 [I]# THIS NEEDS TO BE THE LINE COUNT OF THE FILES INSIDE THE LOOP[/I]
   for ((a=1; a <= $COUNT; a++))
   do
        for txtFiles in *; do
        USERNAME=`cat $PATHTOHARDWARE$txtFiles | grep Logged | awk -F '\'  '{print $2}'`
    echo $USERNAME [I]#just to see if we are getting the correct info.[/I]
        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.

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.

HI There!

Thanks for the response.

I am getting a syntax error on the $(( NextNumber++ )).

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

HI There!

Thanks for the response.

I am getting a syntax error on the $(( NextNumber++ )).

This article has been dead for over six months. Start a new discussion instead.