The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
can someone help me with modifying this file eamani_sun Shell Programming and Scripting 2 05-22-2008 01:15 PM
Modifying a csv file from Shell Script not4google Shell Programming and Scripting 2 11-21-2006 06:47 AM
Final Output charbel Shell Programming and Scripting 4 06-29-2006 12:38 PM
Modifying the URL to point to another location in a .sh UNIX file pjanakir UNIX for Dummies Questions & Answers 6 01-25-2006 03:19 PM
Modifying binary file by editing Hex values ? Nicol UNIX for Advanced & Expert Users 4 11-04-2005 08:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2006
charbel charbel is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 41
Modifying the final output file

Hey all,

I am haivng n number of files all of them are of the same format but different details. i.e

File1 is having the folloeing details:
"Account1",123
"Account2",10
"Account3",12355
"Accountn",555

File2 is having the folloeing details:
"Account1",1234
"Account2",100
"Account3",2290
"Accountn",679

and so on.....so as you can see i am having n number of files, all files have the same account number inside and the only difference is in the number of recrods each account has in each file.....Now what i want to do is to generate a final file which will have all the accounts mentioned earlier along with their records, it will be somehow i a table format.i.e.:

Final output file should look like the following:
"Account1",123,1234
"Account2",10,100
"Account3",12355,2290
"Accountn",555,679

Any idea on how to do this?
  #2 (permalink)  
Old 10-10-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
try something like this

Code:
awk -F"," ' 
{ arr[$1]=arr[$1] FS $2 }
END{
for key in arr
{
print key "," arr[key]
}
}' *
  #3 (permalink)  
Old 10-10-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
anbu23,
have you tried you solution?

nawk -f char.awk file2.txt file1.txt

char.awk:
Code:
BEGIN {
  FS=OFS=","
}
FNR==NR { arr[ $1 ] = $2; next }
{ print $0, arr[ $1 ] }

Last edited by vgersh99; 10-10-2006 at 12:24 PM..
  #4 (permalink)  
Old 10-10-2006
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Code:
join -t"," file1 file2
  #5 (permalink)  
Old 10-10-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
A variation of anbu23's script, which is what I think he was attempting (less syntax errors )
Code:
nawk -F"," '
    BEGIN { FS=OFS="," }

    { arr[ $1 ] = (arr[ $1 ]) ? arr[ $1 ] FS $2 : $2 }

END { for (i in arr) print i, arr[i] }
' file_1 file_2 file_n
This solution allows for any number of files beyond file 2.
  #6 (permalink)  
Old 10-10-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by vgersh99
anbu23,
have you tried you solution?

nawk -f char.awk file2.txt file1.txt

char.awk:
Code:
BEGIN {
  FS=OFS=","
}
FNR==NR { arr[ $1 ] = $2; next }
{ print $0, arr[ $1 ] }
I didn't test my code.
Sponsored Links
Closed Thread

Bookmarks

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 03:28 AM.


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