Input file is comma delimited:
$ cat /tmp/file.txt
5232,92338,84545,34,
2233,25644,23233,23,
6211,1212,4343,434,
2434,621171,9121,33,
Required:
Convert the above comma(,) delimited file to a colon(:) delimited file such that there is no colon at the end of each line.
Python solution:
$ python
Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fp = open("/tmp/file.txt.new","w")
>>> for line in open('/tmp/file.txt'):
... fp.write(line.strip()[:-1].replace(',',':')+'\n')
...
>>>
Output:
$ cat /tmp/file.txt.new
5232:92338:84545:34
2233:25644:23233:23
6211:1212:4343:434
2434:621171:9121:33
Alternative solutions:
An alternative using UNIX sed will be:
$ sed -e 's/,/:/g' -e 's/:$//g' /tmp/file.txt
And a related post using UNIX awk can be found on my bash scripting blog here
0 Comments:
Post a Comment