Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of location of words

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I want to do the following:

I have a textfile with a few pages of text,
and I'd like to get as output all the words alphabetticaly sorted, with the linenumbers and pagenumbers after it.

Like this:

morning 1(5) 2(3)
the 1(2) 1(3) 1(4) 2(3)

so morning is on line 5 on page 1 and so on...

The lines per page are given by the command line:

sh script.awk 10 document.txt (10 lines)

Can anyone please help me? Thanks!
 
Right now I have the following code:

awk '{$0 = tolower($0)
gsub(/[^[:alnum:]_[:blank:]]/, "", $0)
for (i=1; i<=NF; i=i+1)
if (NR%'$1'==0)
{print $i,((NR - NR%'$1')/'$1'), &quot;(&quot;'$1'&quot;)&quot;}
else
{print $i,((NR - NR%'$1')/'$1')+1, &quot;(&quot;NR%'$1'&quot;)&quot;}}' $2

The '$1' is the number of lines per page, the '$2' is the name of the textfile.

The output is now like this:

() is pagenumber

cow 6(12)
cow 1(2)
cow 2(3)
morning 2(10)
morning 2(11)
morning 3(5)
morning 3(4)

When I sort it, it sorts alphabettically...

Now I need to get it like this:

cow 1(2) 2(3) 6(12)
morning 3(4) 3(5) 2(10) 2(11)

Words sorted, then lowest pagenumbers first

Could anyone help me?

Thanks

 
bird 5(2)
bird 7(1)
bird 8(2)
bird 9(3)
bug 1(2)
cow 10(2)
cow 10(4)
cow 11(3)
cow 12(4)

Could anyone tell how I can make from this this:

bird 5(2) 7(1) 8(2) 9(3)
bug 1(2)
cow 10(2) 10(4) 11(3) 12(4)

(When there are more then x(y) things, the things have to go on on the next line)

Thanks!
 
bird 5(2)
bird 7(1)
bird 8(2)
bird 9(3)
bug 1(2)
cow 10(2)
cow 10(4)
cow 11(3)
cow 12(4)

Could anyone tell how I can make from this this:

bird 5(2) 7(1) 8(2) 9(3)
bug 1(2)
cow 10(2) 10(4) 11(3) 12(4)

(When there are more then x(y) things, the things have to go on on the next line)

Thanks!
 
Try this
Code:
{
  if ($1 != sv1) {
    if (NR > 1) print s
    s = $0
    sv1 = $1
  }
  else {
    s = s &quot; &quot; $2
  }
}
END { print s}
Hope this helps. CaKiwi
 
Great!
Thanks, it works...

My only problem left is that the line must be
breaked after 15 references.

so:

cow 1(5) 2(5) ....... till 15(5)
16(5) <-- on next line

Anyone knows how to implement this in the last script?

Thanks!
 
How about
Code:
{
  if ($1 != sv1) {
    if (NR > 1) print s
    s = $0
    sv1 = $1
    j = 1
  }
  else {
    j++
    if (j>15) {
      print s
      s = &quot;&quot;
      j=1
    }
    s = s &quot; &quot; $2
  }
}
END { print s}
Hope this helps. CaKiwi
 
Thanks a lot! You made me very happy :)

(mensen van cosytel: ik heb hier om advies gevraagd, aub mijn code niet overnemen)
 
You're welcome. But I hope you weren't swearing at me in that last sentence. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top