MODE function in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting MODE function in awk
# 8  
Old 10-07-2008
Thanks a lot guys,.

Sorry I am new in this area Smilie,i need some more help ...

How can incorporate yours codes in this code below?Smilie

Code:
 
#!/bin/sh
find . -name '*.txt' | while read line
do
gawk -F '\t' '$2== "A" {print $1"\t" $3"\t" Mode($7,$8,$9,$10)"\t" }' $line
done >out.txt

thanks
# 9  
Old 10-07-2008
Quote:
Originally Posted by jim mcnamara
radulov - it is bimodal, really. It should return a set: (1,2). I don't know what Excel does, just what statistics does. Mode is often used for data like 'birthdays of people in the room' or 'last names of people in a town' - data unlike numbers or numeric data with no real inherent meaning, just magnitude. Football (soccer) scores qualify in that category.

Thanks Jim,
I was not sure if it was supposed to return N/A or a set.
If I'm not missing something again:

Code:
% cat file
32,45,56,32,32
32,45,56,30,92
1,1,2,2
% awk -F, '{ print mode($0) }
func mode(list, sep,  n, a, i, h, m, k, f, r) {
  sep = sep ? sep : FS
  n = split(list, a, sep)
  while (++i <= n) if (++h[a[i]] > m) m = h[a[i]]
  for (k in a) m == h[a[k]] && f[a[k]]
  if (m > 1) for (k in f) r = r ? r sep k : k
  return r ? r : "#N/A"
  }' file
32
#N/A
1,2

Some awk interpreters (eg. mawk) do not support the abbreviated form func (you should use function instead).

Last edited by radoulov; 10-07-2008 at 04:58 PM..
# 10  
Old 10-07-2008
Quote:
Originally Posted by Needhelp2
Thanks a lot guys,.

Sorry I am new in this area Smilie,i need some more help ...

How can incorporate yours codes in this code below?Smilie

Code:
 
#!/bin/sh
find . -name '*.txt' | while read line
do
gawk -F '\t' '$2== "A" {print $1"\t" $3"\t" Mode($7,$8,$9,$10)"\t" }' $line
done >out.txt

Could you post a sample from your input file (*.txt => $line) and an example of the desired output?
# 11  
Old 10-08-2008
hi guys,


i have N txt files , which looks like this:
I
nput file:
Quote:
N TypeA TypeB TypeC
1 23 56 50
2 23 56 50
3 30 36 30
4 20 25 20
5 23 50 50
Wanted output:

Quote:
N TypeA TypeB TypeC Mode
1 23 56 50 #N/A
2 23 56 50 #N/A
3 30 36 30 30
4 20 25 20 20
5 23 50 50 50
# 12  
Old 10-08-2008
Is there a MODE function in awk or Perl ?
So i can call in my script below.
This code below didnt work.



Code:
 
#!/bin/sh
find . -name '*.txt' | while read line
do
gawk -F '\t' '$2== "A" {print $1"\t" $3"\t" Mode($7,$8,$9,$10)"\t" }' $line
done >out.txt


thanks
# 13  
Old 10-08-2008
Try this:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk>out.txt 'FNR == 1 || $0 = $0 FS mode($0) 
func mode(list, sep,  n, a, i, h, m, k, f, r) {
  sep = sep ? sep : FS
  n = split(list, a, sep)
  while (++i <= n) if (++h[a[i]] > m) m = h[a[i]]
  for (k in a) m == h[a[k]] && f[a[k]]
  if (m > 1) for (k in f) r = r ? r sep k : k
  return r ? r : "#N/A"
  }' file1 file2 ... filen


Last edited by radoulov; 10-09-2008 at 03:00 AM.. Reason: removed wrong indication ... (too tired? :))
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. HP-UX

From a C++ application how to find if a hpux host is in standard mode or trusted mode

is there a way for my C++ application to find out which mode the hpux OS is running in? standard mode or trusted mode. (3 Replies)
Discussion started by: einsteinBrain
3 Replies

3. Shell Programming and Scripting

awk function

hi, I have used awk command to delimit my variable. But this that not worked. Could you please let me know what need to be changed in my awk command Input: home/unix>cat test.txt DAILY.JOB CHENNAI,8388 DAILY.JOB BANGLORE,3848 DAILY.JOB TRICHY,9489 DAILY.JOB TIRUPUR,8409 code ... (9 Replies)
Discussion started by: arun888
9 Replies

4. UNIX for Dummies Questions & Answers

A Freebie Text Mode Plot, (Drawing), Function...

This is probably common knowledge to the big guns but this is for newbies... This is a demo using simple plot function using bash. In a default Terminal... It clears the window, sets the cursor to the top left hand corner, draws a triangle inside the window, writes a string inside the... (0 Replies)
Discussion started by: wisecracker
0 Replies

5. Shell Programming and Scripting

awk function

Hi all, I need to have informations in a URL : https://www.autolib.eu/stations/ Valors I need are in bold: {"charging_status": "nonexistent", "rental_status": "future", "subscription_status": "nonexistent", "station_id": 791, "address": "10 rue de Rome, 93110 Rosny-sous-Bois", "lat":... (3 Replies)
Discussion started by: roulitto
3 Replies

6. Solaris

DNS service is in maintenance mode. How to bring it back to online mode?

:confused: when i tried to look the status of DNS-client, it is in maintenance mode..... Please tell me how to bring it back to online mode...PLEASE TELL ME STEP BY STEP.... PLEASE... :wall: (2 Replies)
Discussion started by: vamshigvk475
2 Replies

7. UNIX for Advanced & Expert Users

What is the difference between single line mode and multiline mode in Regular expressions?

Hi All, Can please let me know what is the difference between the single line mode and multi line mode in regular expresions? Thanks, Chidhambaram B (3 Replies)
Discussion started by: chidhu.anu
3 Replies

8. Programming

which function copies data from user to kernel mode

when transitionaning from user to kernel mode which function copies data from user mode buffer to kernel mode? (5 Replies)
Discussion started by: rupeshkp728
5 Replies

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. SuSE

Convet Linux OS from text mode to graphic mode

Hi All, I used to have my suse linux(VM) server in graphic mode but not anymore since morning. I cant rolback since i loose somuch work. Any idea how to it back to normal. Thanks (6 Replies)
Discussion started by: s_linux
6 Replies
Login or Register to Ask a Question