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
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 03:24 PM
Enviornment Variable in B shell (I call it nested variable) princelinux Shell Programming and Scripting 4 07-02-2008 02:35 AM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 03:56 PM
Export command giving Variable Name vs the Value set for the Variable ParNone UNIX for Dummies Questions & Answers 2 04-03-2006 12:43 PM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 12:29 PM

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 08-03-2008
RisingSun RisingSun is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
awk using ssh variable?

I have a file named Atoms that has a list of atoms listed vertically, like:

O
C
Na

etc. There is a variable number of them.

I want to count their occurences in another file. I want to do this by saving each atom as a variable, preferabbly in an associative array, then counting how many there are in the other file. I already can count the number of atoms, and I save that into $numatoms

My problem right now is the awk command recognizing the variable $i from the for loop. The part where it says NR=="$i" doesn't seem to work. Also, afterward, I still wouldn't know how to count the occurences in another file, which is also based on a variable, titled $name.axyz. I'm fairly new, and help would be appreciated!!! Thanks.


for (( i=1; i < $numatoms; i++ )); do
count[$i]=$(awk {'NR=='"$i"'{ print "$1"}' Atoms)
echo $count[$i]
done
  #2 (permalink)  
Old 08-03-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,448
Use only awk:

Code:
awk '{a[$0]++}END{for (i in a) printf "%s\t%s %s\n",i,a[i],"Atoms"}' file > new_file

  #3 (permalink)  
Old 08-04-2008
RisingSun RisingSun is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
Post

Sorry, I'm really new to awk, could you explain your line a little? I tried it and it didn't work. What is meant by file and new_file for, is that where the searching is done? Thanks
  #4 (permalink)  
Old 08-04-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,448

Code:
awk '{
a[$0]++                               #read each line($0) from source file in array a and count ++ occurrences of $0
}
END                                   #at the end of file do..
{
for (i in a)                          #for each member (i)of array a
printf "%s\t%s %s\n",i,a[i],"Atoms"   #format the output, read man printf for more options
}
' file > new_file                     #declare the source file and set the output (>) to new_file

Closed Thread

Bookmarks

Tags
count, unique

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 06:00 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