Solved

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

Posted on 2014-01-09
1
1,063 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 83

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now