[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-01-09
1
Medium Priority
?
1,120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

656 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