changing the format of CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting changing the format of CSV file
# 1  
Old 12-09-2008
Question changing the format of CSV file

Hi Experts,
Please help me to get the following from script for Unix ENvironment(shell, perl, tr, sed, awk).
INPUT FILE:
20K,ME,592971
20K,YOU,2
20K,HE,1244998
50K,YOU,480110
50K,ME,17
50K,HIS,10
50K,HE,1370391

OUTPUT FILE:
K,ME,YOU,HE,HIS
20K,592971,2,1244998,0
50K,17,480110,1370391,10

Egarly waiting for response, Thanks a Ton in Advance
Smilie, Smilie
# 2  
Old 12-09-2008
looks same as of your previous post "Matching fields of rows and then operating"
# 3  
Old 12-09-2008
Quote:
Originally Posted by manikantants
looks same as of your previous post "Matching fields of rows and then operating"
Its not the same, in that post it was to find out first field and add rest field if first field is comman, so it was a mathametical operation.

This post is to change the format of the output file.

Thanks for having a look on it, without solution.

It is often the case that the man who can't tell a lie thinks he is the best judge of one.
# 4  
Old 12-09-2008
I knew its not exactly same. There it was addition based on first column, in this case it is concatenation. Thats the only difference. I am sure you can make use of old script
# 5  
Old 12-09-2008
Code:
open FH,"<a.txt";
while(<FH>){
	@arr=split(",",$_);
	map {tr/\n//d} @arr;
	$hash{$arr[0]}->{$arr[1]}=$arr[2];
}
print "K,ME,YOU,HE,HIS\n";
for $key (keys %hash){
	print $key,",",($hash{$key}->{ME})?$hash{$key}->{ME}:0,",",($hash{$key}->{YOU})?$hash{$key}->{YOU}:0,",",($hash{$key}->{HE})?$hash{$key}->{HE}:0,",",($hash{$key}->{HIS})?$hash{$key}->{HIS}:0,"\n";
}

# 6  
Old 12-09-2008
Thanks Summer_cherry, Thanks a Lot.
The code worked and gave the expected output, Trying to understand perl....but little kno with shell and unix utility awk,sed,tr.
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Changing the file name format

Hello all, I am tryign to change the format of files (which are many in numbers). They at present are named like this: SomeProcess_M-130_100_1_3BR.root SomeProcess_M-130_101_2_3BX.root SomeProcess_M-130_103_3_3RY.root SomeProcess_M-130_105_1_3GH.root SomeProcess_M-130_99_1_3LF.root... (7 Replies)
Discussion started by: emily
7 Replies

2. Shell Programming and Scripting

Changing format of file with awk

Hi all, I have a file that looks like this: Closest words to: manifesto >>>> Closest words to: passport >>>> and I want to reformat this with awk with the following desired result: manifesto 0.99999999999999978, 'manifesto' 0.72008211381623111, 'communiqu\xe9'... (5 Replies)
Discussion started by: owwow14
5 Replies

3. Shell Programming and Scripting

Changing date format in CSV file

I have a CSV file with a date format like this; 11/19/2012 17:37:00,1.372,121.6 11/19/2012 17:38:00,0.743,121.6 Want to change the time stamp to seconds after 1970 so I can get the data in rrdtool. For anyone interested, this is data from a TED5000 unit and is Kwatts and volts. Needs to... (3 Replies)
Discussion started by: ottsm
3 Replies

4. Shell Programming and Scripting

Command for changing date format in a file

Hi... I have an inputfile name as :- abc_test_20120213.dat (date in yyyymmdd format) I need the output file name as abc_test_13022012.dat (date in ddmmyyyy format) Please help me on this... Thanks in advance. (5 Replies)
Discussion started by: gani_85
5 Replies

5. Shell Programming and Scripting

Changing dates in a csv file

Hello, Is there a script template out there that will assist me on creating a script to search for dates with "2011" and change it to "2012" in an excel spreadsheet. I am in Ksh :confused: Thank you, Bryan (1 Reply)
Discussion started by: BrutalBryan
1 Replies

6. Shell Programming and Scripting

changing csv file contents to file of rows

i have a file a.txt contents as 1,2,3,4,......etc...in a single line, i want to write to another file in rows as 1 2 3 4 5 can u help? i do not know the length of a.txt (4 Replies)
Discussion started by: pravfraz
4 Replies

7. Shell Programming and Scripting

Changing the text file format

Hi, I have a shell script to unload all the empname who have salary >50000 from the emp table into a text file(empname.txt) . m_db unload "$dbc_file" -column_delimiter ',' -select "SELECT empname FROM emp where salary > 50000" >> empname.txt Now my text file have data in the following format ... (3 Replies)
Discussion started by: kavithakuttyk
3 Replies

8. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

9. UNIX for Dummies Questions & Answers

Changing display and format of file

I have an input file which looks like this: 601 a 602 a 603 a 601 b 610 c 615 c 603 d 601 d 612 d I need the utput to look like this 601 a 602 603 602 a 601 603 603 a 601 602 601 b 610 c 615 615 c 610 603 d 601 612 (1 Reply)
Discussion started by: wahi80
1 Replies

10. Shell Programming and Scripting

Changing a CSV file in Korn shell

Hi All, Please can someone help me how I can read a CSV file with 10 columns and remove the 2nd, 4th, 6th, 8th column and build the new output file. Script to be done in Korn shell. File contains data like bnt, lb2, lb3, vnw, lb4, lb5, bst, lb6, lb7, vgw (multiple rows) Output file should... (2 Replies)
Discussion started by: riteshm
2 Replies
Login or Register to Ask a Question