Sponsored Content
Top Forums Shell Programming and Scripting Word Occurrences script using awk Post 302923269 by ksmarine1980 on Friday 31st of October 2014 10:58:16 PM
Old 10-31-2014
Huge improvement, thank you Chubler! The only issue's remaining are the alignment.
-The index heading is left aligned, but the index numbers are right aligned (I'd like to get both left aligned)
-The word heading and results are left aligned (need right aligned)
-The work count and results are left aligned (need right aligned).

Also, is there any way to do the sort using the asorti function? It was recommended I use that.

Again, thank you so much for your help!

---------- Post updated at 09:58 PM ---------- Previous update was at 02:43 PM ----------

I've completely redone the script because I wasn't using the actual index values (which this needs to be sorted by). I've come up with the following, which seems close to working, but isn't quite there. I've spent the past 4 hours on this, and am completely at my wits end. Any help would be appreciated. Thanks.

Code:
{
j = 1
for (i in freq)
ind[j] = i
j++
}
{
$0 = tolower($0)
for (i = 1; i <= NF; i++ )
freq [$i]++
}
BEGIN { printf "%-5s %20s %6s\n", "Index", "Word", "Count"}
END {
        asorti(freq)
        for (word in freq)
        printf "%-5s %20s %6s\n", ind[j], word, freq[word]
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

2. Shell Programming and Scripting

awk and gsub - how to replace only the first X occurrences

I have a text (text.txt) and I would like to replace only the first 2 occurrences of a word (but I might need to replace more): For example, if text is this: CAR sweet head hat red yellow CAR book brown tiger CAR cow CAR CAR milk I would like to replace the word "CAR" with word... (12 Replies)
Discussion started by: bingel
12 Replies

3. Homework & Coursework Questions

Du without directory and Grep for occurrences of a word

Assistance on work Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Files stored in ... (1 Reply)
Discussion started by: alindner
1 Replies

4. Shell Programming and Scripting

Count occurrences in awk

Hello, I have an output from GDB with many entries that looks like this 0x00007ffff7dece94 39 in dl-fini.c 0x00007ffff7dece97 39 in dl-fini.c 0x00007ffff7ab356c 50 in exit.c 0x00007ffff7aed9db in _IO_cleanup () at genops.c:1022 115 in dl-fini.c 0x00007ffff7decf7b in _dl_sort_fini (l=0x0,... (6 Replies)
Discussion started by: ikke008
6 Replies

5. Shell Programming and Scripting

Script to count word occurrences, but exclude some?

I am trying to count the occurrences of ALL words in a file. However, I want to exclude certain words: short words (i.e. <3 chars), and words contained in an blacklist file. There is also a desire to count words that are capitalized (e.g. proper names). I am not 100% sure where the line on... (5 Replies)
Discussion started by: Cronk
5 Replies

6. UNIX for Dummies Questions & Answers

BASH - Counting word occurrences in a Web Page

Hi all, I have to do a script bash (for university) that counts all word occurrences in a specific web page. anyone can help me?. Thanks :) (1 Reply)
Discussion started by: piacentero
1 Replies

7. UNIX for Dummies Questions & Answers

Awk: Counting occurrences between two files

Hi, I have two text files (1.txt and 2.txt). 2.txt contains two columns which are extracted from 1.txt using a simple if(condition) print. I want to: - count how many times the values contained in 2.txt appear in 1.txt -if they appear just one time, I have to delete the entire row in... (5 Replies)
Discussion started by: Pintug
5 Replies

8. Shell Programming and Scripting

awk Group By and count string occurrences

Hi Gurus, I'm scratching my head over and over and couldn't find the the right way to compose this AWK properly - PLEASE HELP :confused: Input: c,d,e,CLICK a,b,c,CLICK a,b,c,CONV c,d,e,CLICK a,b,c,CLICK a,b,c,CLICK a,b,c,CONV b,c,d,CLICK c,d,e,CLICK c,d,e,CLICK b,c,d,CONV... (6 Replies)
Discussion started by: Royi
6 Replies

9. UNIX for Advanced & Expert Users

Find 2 occurrences of a word and print file names

I was thinking something like this but it always gets rid of the file location. grep -roh base. | wc -l find . -type f -exec grep -o base {} \; | wc -l Would this be a job for awk? Would I need to store the file locations in an array? (3 Replies)
Discussion started by: cokedude
3 Replies

10. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies
Text::Aligner(3pm)					User Contributed Perl Documentation					Text::Aligner(3pm)

NAME
Text::Aligner SYNOPSIS
use Text::Aligner qw( align); # Print the words "just a test!" right-justified each on a line: my @lines = align( 'right', qw( just a test!); print "$_ " for @lines; DESCRIPTION
Text::Aligner exports a single function, align(), which is used to justify strings to various alignment styles. The alignment specification is the first argument, followed by any number of scalars which are subject to alignment. The operation depends on context. In list context, a list of the justified scalars is returned. In scalar context, the justified arguments are joined into a single string with newlines appended. The original arguments remain unchanged. In void context, in-place justification is attempted. In this case, all arguments must be lvalues. Align() also does one level of scalar dereferencing. That is, whenever one of the arguments is a scalar reference, the scalar pointed to is aligned instead. Other references are simply stringified. An undefined argument is interpreted as an empty string without complaint. Alignment respects colorizing escape sequences a la Term::ANSICOLOR, which means it knows that thses sequences don't take up space on the screen. ALIGNMENT
The first argument of the align() function is an alignment style, a single scalar. It can be one of the strings "left", "right", "center", "num", "point", or "auto", or a regular expression (qr/.../), or a coderef. A default style of "left" is assumed for every other value, including "" and undef. "left", "right" and "center" have the obvious meanings. These can also be given as numbers 0, 1, and 0.5 respectively. (Other numbers are also possible, but probably not very useful). "num", and its synonym "point", specify that the decimal points be aligned (assumed on the right, unless present). Arbitrary (non-numeric) strings are also aligned in this manner, so they end up one column left of the (possibly assumed) decimal point, flush right with any integers. For the occasional string like "inf", or "-" for missing values, this may be the right place. A string-only column ends up right-aligned (unless there are points present). The "auto" style separates numeric strings (that are composed of "-", ".", and digits in the usual manner) and aligns them numerically. Other strings are left aligned with the number that sticks out farthest to the left. This gives left alignment for string-only columns and numeric alignment for columns of numbers. In mixed columns, strings are reasonably placed to serve as column headings or intermediate titles. With "num" (and "point") it is possible to specify another character for the decimal point in the form "num(,)". In fact, you can specify any string after a leading "(", and the closing ")" is optional. "point(=>)" could be used to align certain pieces of Perl code. This option is currently not available with "auto" alignment (because recognition of numbers is Anglo-centric). If a regular expression is specified, the points are aligned where the first match of the regex starts. A match is assumed immediately after the string if it doesn't match. A regular expression is a powerful way of alignment specification. It can replace most others easily, except center alignment and, of course, the double action of "auto". POSITIONERS
For entirely self-defined forms of alignment, a coderef, also known as a positioner, can be given instead of an alignment style. This code will be called once or more times with the string to be aligned as its argument. It must return two numbers, a width and a position, that describe how to align a string with other strings. The width should normally be the length of the string. The position defines a point relative to the beginning of the string, which is aligned with the positions given for other strings. A zero position for all strings results in left alignment, positioning to the end of the string results in right alignment, and returning half the length gives center alignment. "num" alignment is realized by marking the position of the decimal point. Note that the position you return is a relative measure. Adding a constant value to all positions results in no change in alignment. It doesn't have to point inside the string (as in right alignment, where it points one character past the end of the string). The first return value of a positioner should almost always be the length of the given string. It may be useful to ly about the string length if the string contains escape sequences that occupy no place on screen. USAGE
use Text::Aligner qw( align); align( $style, $str, ...); $style must be given and must be an alignment specification. Any number of scalars can follow. An argument that contains a scalar reference is dereferenced before it is used. In scalar and list context, the aligned strings are returned. In void context, the values are aligned in place and must be lvalues. BUGS
None known as of realease, but... AUTHOR
Anno Siegel CPAN ID: ANNO COPYRIGHT
Copyright (c) 2002 Anno Siegel. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. SEE ALSO
perl(1) Text::Table perl v5.10.1 2011-06-10 Text::Aligner(3pm)
All times are GMT -4. The time now is 02:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy