How often a name comes up for a certain row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How often a name comes up for a certain row
# 1  
Old 01-02-2010
How often a name comes up for a certain row

Hi,

I have a file that contains multiple rows and I want to find how often a list of names come up for row #2 (note: all files are tab deliminted):

So the file looks like this:
Code:
1212  RICK
9823  JOAN
1221  RICK
1254  JACK
1224  JACK

So I have another file that contains the list of names on column 1 (also has multiple columns) and it looks like thsi

Code:
RICK  YUI
JOAN  JUR
JACK  NOM

It would be great if i could get the output to look like this

Code:
RICK  2
JOAN  1
JACK  2

thanks and happy new year
# 2  
Old 01-02-2010
Code:
$ cat file1
1212  RICK
9823  JOAN
1221  RICK
1254  JACK
1224  JACK

$ cat file2
RICK  YUI
JOAN  JUR
JACK  NOM

$ awk 'NR==FNR{a[$2]++} NR>FNR{print $1,a[$1]}' file1 file2
RICK 2
JOAN 1
JACK 2

# 3  
Old 01-02-2010
MySQL

Here is the expected result. I created the awk command for a similar requirement. Hope it helps.

Since I am a newbie in UNIX shell scripting, I would appreciate the feedback of other folks if the commands are maintainable and well-written.

Code:
bash-2.05$ cat names.txt
1212  RICK
9823  JOAN
1221  RICK
1254  JACK
1224  JACK
bash-2.05$ awk {'print $2'} names.txt | sort | awk 'BEGIN{cntr=0;prevLine=""}{if(prevLine==""){prevLine=$0};if($0==prevLine){cntr++;}else{printf prevLine;print " " cntr;cntr=1;}prevLine=$0;}END{printf prevLine;print " " cntr}'
JACK 2
JOAN 1
RICK 2
bash-2.05$

# 4  
Old 01-02-2010
Quote:
Originally Posted by phil_heath
It would be great if i could get the output to look like this

Code:
RICK  2
JOAN  1
JACK  2

Base on your required output I don't see why you need the second file Smilie
Code:
awk '{a[$2]++}END{for(i in a) print i,a[i]}' file1



---------- Post updated at 08:43 AM ---------- Previous update was at 08:41 AM ----------

Maybe you want:
Code:
# awk 'NR==FNR{a[$2]++;next}{$0=$0 FS a[$1]}1' file1 file2
RICK  YUI 2
JOAN  JUR 1
JACK  NOM 2

# 5  
Old 01-02-2010
a perl solution.

Code:
while (<DATA>)  {
    $_ =~ s/^([^\s]*) .*/\1/g;    
    chomp;

    $thash{$_} = 0;
}

while ( <> )  {
    $required = (split /[ \t]/, $_)[1];    
    chomp($required);    

    $thash{$required}++;    
}

foreach ( keys %thash )  {
    print "$_ $thash{$_}\n";
}

__DATA__
RICK YUI
JOAN JUR
JACK NOM

Execute it like:
Code:
perl file.pl < file1
RICK 2
JACK 2
JOAN 1

# 6  
Old 01-04-2010
you can try this..

Code:
# cat file1
1212  RICK
9823  JOAN
1221  RICK
1254  JACK
1224  JACK
# sort -k2 file1 | uniq -c -f1 | awk '{print $3,$1}'
JACK 2
JOAN 1
RICK 2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

4. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

5. Shell Programming and Scripting

Comparing two files on row by row and send the report

Comparing two files on row by row File1 ecount~100 dcount~200 ccount~300 zxcscount~5000 and so on. File2 ecount~100 dcount~203 ccount~300 zxcscount~5000 and so on. If i use diff command (1 Reply)
Discussion started by: onesuri
1 Replies

6. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

7. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

8. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

9. UNIX for Dummies Questions & Answers

How to calculate the percentage/fraction of each value in a row against the maximum row value?

Hi, For each row in a file, i would like to identify the maximum value and calculate the percentage/fraction of the max for other values in the row. Then, I would like to print a copy of the file where values above a threshold are replaced with "1" and other values are left as "0". In the... (2 Replies)
Discussion started by: auburn
2 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question