Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

821 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