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
Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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 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…
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…

840 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