Computing.Net > Forums > Programming > Batch needed to add column to CSV

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch needed to add column to CSV

Reply to Message Icon

Name: majinfaisal
Date: September 9, 2007 at 14:06:01 Pacific
OS: vista
CPU/Ram: 2gig
Product: AMD opteron 165
Comment:

Hi,
I require some help in modifying a csv file using a batch script.

I have a csv file that contains details obtained from a database. So an example construct is as follows: -

id, first_name, second_name, location
1, john, locke, 123.someroad
2, jack, shepard, 555.island
3, abc, khan, 888.house
4, popeye, sailor, 143.boathouse

What i need the batch script to do is to create another column in the csv file (housen in this example) and to move the digits in the location before the period character into that column. So the output will be like: -

id, first_name, second_name, location, housen
1, john, locke, someroad, 123
2, jack, shepard, island, 555
3, abc, khan, house, 888
4, popeye, sailor, boathouse,143

I am rather new to batch scripting so not exactly sure how to tackle this. All help will be very greatly appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: September 9, 2007 at 14:27:30 Pacific
Reply:

Assumed your .csv file is data.csv, then

@Echo Off > data-new.csv
For /F "tokens=1-5 delims=,. " %%a in (data.csv) Do (
Echo %%a, %%b, %%c, %%e, %%d>> data-new.csv
)

and data-new.csv will hold the new format.


0

Response Number 2
Name: Mechanix2Go
Date: September 9, 2007 at 14:42:44 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=1-4 delims=, " %%a in (my.csv) do (
set /a N+=1
if !N! equ 1 (echo id, first_name, second_name, location, housen > new.csv
) else (
for /f "tokens=1-2 delims=." %%P in ('echo %%d') do (
set st=%%Q & set hn=%%P
)
echo %%a, %%b, %%c, !st!, !hn! >> new.csv
)
)



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 3
Name: majinfaisal
Date: September 9, 2007 at 14:44:15 Pacific
Reply:

Hi,

Thanks for the help so far.
This successfully pulls out the number from the location and moves this to a seperate column as should, but does not add a column header 'housen' to accompany the new column set. What i think must be done is to add this column name before this code is executed but i am not sure how this is done exactly.

Again many thanks for the help so far.


0

Response Number 4
Name: Mechanix2Go
Date: September 9, 2007 at 14:46:22 Pacific
Reply:

Hi IVO,

I gotta learn to type faster. LOL

Yours is better.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 5
Name: majinfaisal
Date: September 9, 2007 at 14:46:52 Pacific
Reply:

Hi, i have just tested the script by Mechanix2Go and it seems to do the trick!!!

Thanks again for all the help provided into tackling this problem!


0

Related Posts

See More



Response Number 6
Name: majinfaisal
Date: September 9, 2007 at 14:48:03 Pacific
Reply:

BTW is it possible to manipulate the first script to add the housen column? As its much simpler to understand lol


0

Response Number 7
Name: Mechanix2Go
Date: September 9, 2007 at 15:01:20 Pacific
Reply:

@Echo Off > data-new.csv
setLocal EnableDelayedExpansion

For /F "tokens=1-5 delims=,. " %%a in (data.csv) Do (
set /a N+=1
if !N! equ 1 (echo id, first_name, second_name, location, housen > data-new.csv
) else (
Echo %%a, %%b, %%c, %%e, %%d>> data-new.csv
)
)



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 8
Name: Razor2.3
Date: September 9, 2007 at 18:32:01 Pacific
Reply:

Suggested change?

echo id, first_name, second_name, location, housen > data-new.csv
For /F "skip=1 tokens=1-5 delims=,. " %%a in (data.csv) Do Echo %%a, %%b, %%c, %%e, %%d>> data-new.csv


0

Response Number 9
Name: IVO
Date: September 10, 2007 at 05:44:20 Pacific
Reply:

Well coded, Razor2.3.

When I posted my version of the script, in Italy time was so late in night (and after a sunday of hard work) that no way I took care of the presence of a header row.


0

Response Number 10
Name: majinfaisal
Date: September 10, 2007 at 14:40:47 Pacific
Reply:

Hi, i just tried the code by Razor and it only returns the column names but not the data


0

Response Number 11
Name: Razor2.3
Date: September 10, 2007 at 14:58:29 Pacific
Reply:

Seeing how I openly stole the code you acknowledge as working, I'd say the problem is on your end.

My guess, the script can't find data.csv


0

Response Number 12
Name: majinfaisal
Date: September 10, 2007 at 17:01:31 Pacific
Reply:

I seem to have come up with a new problem. if i take the sample data set: -

id, first_name, second_name, location, housen
1,, locke, someroad, 123

and try to parse with comma delimiter i seem to get this: -

1,locke, someroad, 123,,

Essentally empty tokens are being filled with whatever token is next in line. Is there a fix for this? Thanks in advance!


0

Response Number 13
Name: ghostdog
Date: September 10, 2007 at 20:04:39 Pacific
Reply:

here's an alternative:
[code]
Set objFSO=CreateObject("Scripting.FileSystemObject")
myFile = "c:\temp1\a.txt"
outFile = "c:\temp1\b.txt"
Set objFile = objFSO.OpenTextFile(myFile,1)
Set objOutFile = objFSO.CreateTextFile(outFile,True)
objOutFile.Write(objFile.ReadLine & ",Housen" & vbCrLf )
Do Until objFile.AtEndOfLine
splitted = Split(objFile.ReadLine,",")
splitaddr = splitted(3)
number= Split(splitaddr,".")(0)
address=Split(splitaddr,".")(1)
splitted(3) = address
objOutFile.Write(Join(splitted,",") & "," & number & vbCrLf )
Loop
objOutFile.Close
objFile.Close
[/code]


0

Response Number 14
Name: majinfaisal
Date: September 11, 2007 at 00:35:30 Pacific
Reply:

Hi, thanks for the alternative solution.
Im stuck to using regular dos batch scripting though and not vbscript.
it is easier to follow and understand the vbscript example though!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch needed to add column to CSV

Batch file to rename .csv field www.computing.net/answers/programming/batch-file-to-rename-csv-field/19279.html

Batch to add filename to txt file www.computing.net/answers/programming/batch-to-add-filename-to-txt-file/14863.html

Batch file-add column names to CSV www.computing.net/answers/programming/batch-fileadd-column-names-to-csv/15851.html