The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
changing month in Mmm format to mm FORMAT RahulJoshi Shell Programming and Scripting 1 09-04-2008 03:20 AM
Changing display and format of file wahi80 UNIX for Dummies Questions & Answers 1 08-02-2008 09:12 PM
changing format shary Shell Programming and Scripting 4 01-31-2008 05:20 AM
changing the format of date nasirgondal Post Here to Contact Site Administrators and Moderators 1 06-08-2006 02:37 AM
Changing date format Sabari Nath S Shell Programming and Scripting 5 12-07-2005 03:38 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-09-2008
ashis.tewari ashis.tewari is offline
Registered User
  
 

Join Date: Dec 2008
Location: India
Posts: 7
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
,
  #2 (permalink)  
Old 12-09-2008
manikantants manikantants is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 131
looks same as of your previous post "Matching fields of rows and then operating"
  #3 (permalink)  
Old 12-09-2008
ashis.tewari ashis.tewari is offline
Registered User
  
 

Join Date: Dec 2008
Location: India
Posts: 7
Quote:
Originally Posted by manikantants View Post
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 (permalink)  
Old 12-09-2008
manikantants manikantants is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 131
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 (permalink)  
Old 12-09-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092

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 (permalink)  
Old 12-09-2008
ashis.tewari ashis.tewari is offline
Registered User
  
 

Join Date: Dec 2008
Location: India
Posts: 7
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.
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0