Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: O/T - Programming assistance

Author: Daniel Clausen

Date: 12:10:37 08/10/04

Go up one level in this thread


On August 10, 2004 at 14:57:38, Ray Banks wrote:

>My limited programming knowledge is so old that this will take me hours, but I
>know some of you here could do it in 5 mins.
>
>I need to prompt for a file to read in, (a comma delimited text file), calculate
>an additional column, and prompt to write back to a new file. I know it is
>probably a matter of reading the file into an array, calculating and adding the
>extra column with a do-loop, then outpiutting it, but I've forgotten how. I also
>don't have any programming languages currently installed.
>
>The input file is like this:
>
>1,"35000/001",1/1/2002,06:00:00
>2,"35000/001",5/2/2003,07:00:00
>3,"35000/001",6/1/2004,07:00:00
>4,"35000/002",3/9/1999,05:32:00
>5,"35000/002",5/4/2004,04:34:00
>6,"35000/003",4/1/2003,07:12:00
>7,"35000/004",5/5/2001,12:34:00
>8,"35000/004",3/2/2002,17:30:00
>9,"35000/005",9/7/2004,12:32:00
>
>and the output file would be
>
>1,"35000/001",1/1/2002,06:00:00,1
>2,"35000/001",5/2/2003,07:00:00,2
>3,"35000/001",6/1/2004,07:00:00,3
>4,"35000/002",3/9/1999,05:32:00,1
>5,"35000/002",5/4/2004,04:34:00,2
>6,"35000/003",4/1/2003,07:12:00,1
>7,"35000/004",5/5/2001,12:34:00,1
>8,"35000/004",3/2/2002,17:30:00,2
>9,"35000/005",9/7/2004,12:32:00,1
>
>The new last column is basically a counter, which is re-set upon a changed value
>in the second column.
>
>I know it is asking a lot, but I'd be really grateful if someone could give me
>some assistance on this
>
>many thanks
>Ray

If you have a bourne shell (under Linux oder cygwin installed under Windows),
the following script does the job. (the first (and only) argument to the script
is the name of the inputfile, output will be written to stdout)

Sargon

---
#!/bin/sh

cat "$1" | while read line ;do
	currentPart=`echo "$line" | cut -f2 -d,`
	if [ "$part" != "$currentPart" ] ;then
		n=0
	fi
	part="$currentPart"
	n=$[n+1]
	echo "$line,$n"
done



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.