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
How to parse through a file and based on condition form another output file sivasu.india UNIX for Advanced & Expert Users 6 02-28-2008 04:59 AM
awk output to file stonemonolith Shell Programming and Scripting 2 01-19-2008 02:27 AM
Creation of output file from list of file ganapati Shell Programming and Scripting 7 01-18-2008 07:55 PM
compare file size from a output file from a script moustik Shell Programming and Scripting 7 11-07-2007 10:17 AM
appending to sed output of one file into the middle of file go4desperado Shell Programming and Scripting 5 02-05-2007 02:20 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 06-25-2006
charbel charbel is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 41
Output File

Hi there,

I am having an output file resulting from a script, this output file looks as follow:

10 Account1
20 Account2
89 Account3

Now i have another file which includes all the accounts (i.e. all acounts which have records in the mentioned output file and the rest of acoounts which have no records)...what i want to do is the following:

I want to generate a final out put file which have all the accounts and near to each account it mention the total number of recrods resulted from the above output fle, and if there is any account which have no records in the above output file, then in the final output file the value near to that account shall be 0

Any idea how to do that??

Thanks in advance.
  #2 (permalink)  
Old 06-25-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
nawk -f char.awk fileFROMscript fileWithAllAccounts

char.awk:

Code:
# for the FIRST file specified on the command line....
# build array 'arr' indexed by the values from the SECOND column AND 
# the accumulated values from the FIRST column.
FNR==NR {arr[$2] += $1; next }

# for the SECOND file specified on the command line......
# print every record.
# if the value of the FIRST column appears as the INDEX in 'arr' - print the 
# 'value' from 'arr'. Otherwise print '0' .
{
   printf("%d%s%s\n", ($1 in arr) ? arr[$1] : 0, OFS, $1)
}


Last edited by vgersh99; 06-26-2006 at 03:41 PM..
  #3 (permalink)  
Old 06-25-2006
Doc_RuNNeR Doc_RuNNeR is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 17
How will be the output of the file wich has all the accounts?

Accoun1
Account2
.....
.....

I think that i can do with a shell script but it is longer than nawk

Bye
  #4 (permalink)  
Old 06-26-2006
charbel charbel is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 41
It will just include the name of the accounts in one coloum (this file name will be output1).....and as i explained before, i am having also another file resulted from a shell script (output2)which have accounts and their records but for sure not all accounts listed in the above file (output1)....so its like a comparison between output1 & output2......all the accounts mentioned in output2 along with their records will be displayed with no change, for those accounts which are mentioned in output1 but are found in output2 will be diplayed with number "0" displayed near them.......
Hope i explained it in a good way...Thx
  #5 (permalink)  
Old 06-26-2006
charbel charbel is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 41
If you dont mind, can you explain to me how does the below work, i am not that much expert in unix. I want to try it but at least i need to understand what each parameter mean so that i will be able to load it to my script.


Quote:
Originally Posted by vgersh99
nawk -f char.awk fileFROMscript fileWithAllAccounts

char.awk:

Code:
FNR==NR {arr[$2] += $1; next }
{
   printf("%d%s%s\n", ($1 in arr) ? arr[$1] : 0, OFS, $1)
}

Thanks for your help.
  #6 (permalink)  
Old 06-26-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
added comments to the original post.
  #7 (permalink)  
Old 07-02-2006
charbel charbel is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 41
what about if the output files are of the following format:

Account1 10
and so on.....then what fields to be interchanged in your command?
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 08:43 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