AWK quiz, multiple choice


View Poll Results: What does that awk code do with the awk manual page?
The word frequencies, with words grouped by frequency, in descending order. 8 88.89%
Same word average distances (i.e. how many chars in between two same words), with words grouped by avg distance, in descending order. 1 11.11%
The average positions in which a word appears inside a sentence, with words grouped by average position, in descending order. 0 0%
Voters: 9. This poll is closed

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK quiz, multiple choice
# 1  
Old 05-25-2009
AWK quiz, multiple choice

Nobody cared about the original post, or perhaps it was a bit difficult to guess.
Therefore I am including a poll here.

Code:
nawk -F'[^A-Za-z_]+' '
{i=NF;for(;i;i--)if($i)w[tolower($i)]++}
END{
  for(k in w){c=w[k];x[c]=x[c]k",";if(c>m)m=c}
  for(;m;m--)if(m in x)print m,x[m]
  }
' <(man nawk)

# 2  
Old 05-25-2009
Voted for the second option.
I would write it like this:

Code:
nawk -F'[^A-Za-z_]+' '{
  for (i=1; i<=NF; i++) 
    $i && w[tolower($i)]++
    }
END{
  for (k in w) {
    c = w[k]
    x[c] = c in x ? x[c] "," k : k
    (c > m) && m = c
    }
  do 
    if (m in x) print m, x[m] 
      while (m--) 
  }' <(man nawk)

# 3  
Old 05-25-2009
Quote:
Originally Posted by radoulov
Voted for the second option.
I would write it like this:
I like your style, you saved a few characters.

This one tough
Code:
for (i=1; i<=NF; i++)

should be faster this way
Code:
for (i=NF; i; i--)

since usually the zero check is translated to assembly code that is simpler than that of a less than check.

Ok, to be fair, the better readability of the first outweighs by far the infinitesimal advantage in speed of the second.
# 4  
Old 05-25-2009
Quote:
Originally Posted by colemar
[...]
This one tough
Code:
for (i=1; i<=NF; i++)

should be faster this way
Code:
for (i=NF; i; i--)

since usually the zero check is translated to assembly code that is simpler than that of a less than check.[...]
I didn't know that,
thank you for elaborating on how it works.
# 5  
Old 05-25-2009
Cool, I tested the zero check and less than check - zero check seems to real quick compared to the less than check

Thanks for the insight

Code:
awk '{ NF=9999999; for ( i=1; i<=NF; i++ ) { } }' dummy  50.74s user 0.70s system 97% cpu 52.919 total

Code:
awk '{ NF=9999999; for ( i=NF; i; i-- ) { } }' dummy  31.10s user 0.74s system 95% cpu 33.295 total

# 6  
Old 05-26-2009
if its just for timing the iteration from 1 to 999999, then i guess its ok. however, both output are different and may serve different purposes. One counts up and the other counts down. Therefore in such cases, one will look at the whole context , eg if you want to get the 1000th item for example, you might not want to use the decrement method.
# 7  
Old 05-26-2009
yes, its purely for timing purpose - where the result of looping is immaterial of incremeting or decrementing the counter.

As you said, the case for accessing nth element in m possible values where
Code:
m - n

being almost equal to 0, or n very less compared to m/2 or m/4, it doesnt make sense to choose any of the orders here and applying predefined heuristics in direction of incrementing/decrementing will make sense.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple choice quiz im shell not working

Hi, I have to create a quiz im form of bash-shell-script. The problem is: I don't find a way to bring multiple choice questions to work. As long as someone selects only one of the possible answers everything is fine, but if one selects two or more options it doesn't work. The code I used is... (4 Replies)
Discussion started by: Naky
4 Replies

2. Filesystems, Disks and Memory

SSD Caching, how its done, right choice?

Hello, someone needed VPS with SSD caching, he want to use server for websites hosting. What does that mean, this SSD caching and is it optimal solution for this? Also i listen some SSD dont like too much of writting so how one can recognise certain SSD is made the way that its not destroyed... (1 Reply)
Discussion started by: postcd
1 Replies

3. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

4. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

5. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

6. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

7. Shell Programming and Scripting

AWK quiz

How come every post in this forum is a request for help? How about some fun? What does the following command do? nawk -F'+' ' {i=NF;for(;i;i--)if($i)w++} END{ for(k in w){c=w;x=xk",";if(c>m)m=c} for(;m;m--)if(m in x)print m,x } ' <(man nawk) (2 Replies)
Discussion started by: colemar
2 Replies

8. UNIX Desktop Questions & Answers

Window Manager of the ... Choice

Inspired by Window Manager of the Year threads from LinuxQuestions.org Like these: 2002 | 2003 | 2004 | 2005 | 2006 I wonder what WMs are used by UNIX people ... People sometimes select diffrent WMs for (old and slow) laptop and (overpowered) workstation, that is why poll allows multiple... (8 Replies)
Discussion started by: vermaden
8 Replies

9. Shell Programming and Scripting

how to write examination(multiple choice) script in unix

Hi, I am unable to get idea, how to write the script like multiple choice exam model script , how to specify the welcome to the exam(name) and if he select option then it has to enter into the exam , then choosing the answers,next question and going to previous question. Thanks & Regards ... (0 Replies)
Discussion started by: palreddy7
0 Replies
Login or Register to Ask a Question