Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

[awk] Compare two files

$
0
0
HI!!
I am trying to compare two files using AWK but I have some problems. I need to count how many times letters are used in two texts. This is my script
Code:

{
  long=length($0)
 
  for (i=1;i<=long;i++)
      {
      aux=substr($0,i,1)
      if ( aux != " " && aux != "" )
          letter[tolower(aux)]++
          if ( letter[tolower(aux)] > max )
              {
              max=letter[tolower(aux)]
              max_letter=aux
              }
      }
}
END {
  print "Lettera maggiormente utilizzata:"max_letter" Occorrenze:"max
  for (item in letter)
      print "Lettera:"item" Occorrenze:"letter[item]
}

It works well for a file, but I need to have the results in this way:
file n.1 :
file n.2:
It-s important to have a distinction between the results.
I have thought a script like this, but I have some problems with the syntax and I cant merge the two things
Code:

BEGIN {
    n = 0
}
FNR==1 {
    idx = 0
    n += 1
}

{ files[n][idx++]=$0 }

END {
    for(i=1;i<=n; i++)
        for (line in files[i])
            printf "file n.%s: %s\n", i, files[i][line]
}


Viewing all articles
Browse latest Browse all 16232

Trending Articles