# awk using ssh variable?

**URL:** <https://community.unix.com/t/awk-using-ssh-variable/207274>\
**Category:** Shell Programming and Scripting\
**Created:** [August 3, 2008, 4:34pm UTC](https://community.unix.com/t/awk-using-ssh-variable/207274 "2008-08-03T16:34:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RisingSun](https://community.unix.com/letter_avatar/risingsun/32/6_449c533f586939b6e5f246632a46421f.png) [@RisingSun](https://community.unix.com/u/RisingSun)\
**Post date:** [August 3, 2008, 4:34pm UTC](https://community.unix.com/t/awk-using-ssh-variable/207274/1 "2008-08-03T16:34:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/6_449c533f586939b6e5f246632a46421f.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [August 3, 2008, 5:52pm UTC](https://community.unix.com/t/awk-using-ssh-variable/207274/2 "2008-08-03T17:52:19Z")

</div>

Use only awk:

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

```

---

<div class="post-metadata">

**Author:** ![RisingSun](https://community.unix.com/letter_avatar/risingsun/32/6_449c533f586939b6e5f246632a46421f.png) [@RisingSun](https://community.unix.com/u/RisingSun)\
**Post date:** [August 4, 2008, 10:52am UTC](https://community.unix.com/t/awk-using-ssh-variable/207274/3 "2008-08-04T10:52:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/6_449c533f586939b6e5f246632a46421f.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [August 4, 2008, 12:07pm UTC](https://community.unix.com/t/awk-using-ssh-variable/207274/4 "2008-08-04T12:07:48Z")

</div>

```nohighlight
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,"Atoms" #format the output, read man printf for more options
}
' file > new_file #declare the source file and set the output (>) to new_file 

```
