views:

54

answers:

1

Hi i have as text file below

input

326783,326784,402
326783,0326784,402
503534,503535,403
503534,0503535,403
429759,429758,404
429759,0429758,404
409626,409627,405
409626,0409627,405

369917,369916,402
369917,0369916,403

i want to convert it like below

condition :

1)input file column 3 and column 1 should be be same for 326784 and 0326784 and like that so on

2)if it different like the above input file last case then it should be printed in last line

output should be

326783,326784,0326784,402
503534,503535,0503535,403
429759,429758,0429758,404
409626,409627,0409627,405
369917,369916,402
369917,0369916,403

i am using solaris platform

please help me

A: 

I don't understand the logic of your computation, but some general advice: the unix tool awk can do such computations. It understands comma-separated files and you can get it to output other comma-separated files, manipulated by your logic (which you'll have to express in awk syntax).

This is, as I understand it, the unix way to do it. The way I'd do it (being a non-expert on awk and just mentioning it for completeness ;) would be to write a little python script. you want to

  • open an input and an output file
  • get each line from the input file
  • parse the integers
  • perform your logic
  • write integers to your output file

unchecked python-like code:

f_in = open("input", "r")
f_out = open("output", "w")
for line in f_in.readlines():
   ints = [int(x) for x in line.split(",")]
   f_out.write("%d, %d, %d\n" % (ints[0], ints[1], ints[0]+ints[1]))
f_in.close()
f_out.close()

Here, the logic is in the f_out.write(...) line (this example would output the first, the second and the sum of both input integers)

You can check if you have a Python interpreter at hand by simply typing python and seeing what happens. If you have, save your code into something.py and start it with "python something.py"

Nicolas78
looking again at your example, it seems like some across-line logic is necessary. which might make python the more viable choice anyhow (but again, can't really say much about awk, but think it's designed to be line-based or do simple stuff like summing)
Nicolas78