Solved

Add column value to a CSV through a script  depending on anther column

Posted on 2014-01-09
1
1,092 Views
Last Modified: 2014-01-09
I have an csv file that i would like to add another column (R) to. I need it to be dependent on the value in the 6th column (F).

If F=1 I need R = 1
If F=2 I need R = 2
If F= (anything else) I need R = the value in column E

I am attaching an example of the kind of CSV i am working with.

Normally i would have focused on trying to find an VBS that would do this, however my last question was answered with a batch file that worked perfectly. However I am not sure which way is best in this instance.

I am hoping someone can steer me towards a solution or example of something working with csv files in a similar manner.

Thank you,
example2.csv
0
Comment
Question by:sattermc
1 Comment
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39769334
How about this?
@echo off
setlocal enabledelayedexpansion
set CsvFile=example2.csv
set OutFile=example2-new.csv
if exist "%OutFile%" del "%OutFile%"
for /f "delims=" %%C in ('type "%CsvFile%"') do (
	set Line=%%C
	set Line="!Line:,=","!"
	for /f "tokens=1,2,5,6 delims=," %%a in ("!Line!") do (
		set Col1=%%~a
		set Col2=%%~b
		set Col5=%%~c
		set Col6=%%~d
		echo Processing !Col1!, !Col2! ...
		if "!Col6!"=="1" (
			set NewCol=1
		) else (
			if "!Col6!"=="2" (
				set NewCol=2
			) else (
				set NewCol=!Col5!
			)
		)
		>>"%OutFile%" echo %%C,!NewCol!
	)
)
echo Done.

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I extract data from Excel worksheets? 5 51
.dn property wont validate - Powershell 2 22
VB script help 23 30
BatchFile-Disk Detection 23 22
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question