Awk: Counting occurrences between two files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk: Counting occurrences between two files
# 1  
Old 07-02-2013
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 1.txt where they are AND I have also to replace the value of the second column of 2.txt with the value of the first column of 2.txt in third file 3.txt


I get lost with arrays I guess.
I tried with
Code:
NR==FNR {to[$2]++;next}
END{ for(var in to) print var, "appear", to[var]," times"} file 2 file1

but the output is not what I want.


Thanks,
Pintug
# 2  
Old 07-02-2013
Show the input you have and the output you want.
# 3  
Old 07-02-2013
For example I have 1.txt

Quote:
1 2
2 3
1 4
1 5
4 5
4 6
2 7
6 7
2.txt is
Quote:
2 3
1 5
4 5
I should obtain a 1.txt modified , i.e. the row " 2 3 " does not appear because "3" appears just one time in 1.txt
Quote:
1 2
1 4
1 5
4 5
4 6
2 7
6 7

I will then have to replace "3" with "2" all along a third file 3.txt . I can handle this with just one specific string, but here I have to use arrays to run it automatically..


Hope it's clearer now
# 4  
Old 07-02-2013
Do you mean all values in 2.txt or only values in column 2 in 2.txt should not appear only once in 1.txt ?
# 5  
Old 07-02-2013
try:
Code:
awk 'NR==FNR {a[$2]++; next} a[$2]!=1' 2.txt 1.txt

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 07-03-2013
@rdrtx1 got exactly what I had to do!! Thank you so much!!! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Word Occurrences script using awk

I'm putting together a script that will the count the occurrences of words in text documents. It works fine so far, but I'd like to make a couple tweaks/additions: 1) I'm having a hard time displaying the array index number, tried freq which just spit 0's back at me 2) Is there any way to... (12 Replies)
Discussion started by: ksmarine1980
12 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Counting non-specific occurrences within a file.

I'm pretty new to scripting and didn't see an example of this issue yet. I am trying to count and print the total number of times each value is found within a file. Here is a short example of my starting file. value 3 value 3 value 3 value 3 value 4 value 6 value 6 value 6 value 6... (3 Replies)
Discussion started by: funkynmr
3 Replies

5. 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

6. Shell Programming and Scripting

Counting occurrences of all words in multiple files

Hey Unix gurus, I would like to count the number occurrences of all the words (regardless of case) across multiple files, preferably outputting them in descending order of occurrence. This is well beyond my paltry shell scripting ability. Researching, I can find many scripts/commands that... (4 Replies)
Discussion started by: twjolson
4 Replies

7. Shell Programming and Scripting

AWK - Comparing/Matching/Counting with 2 files

I have 2 files that I want to do some comparing on. First, I want to find the unique list of devices in file1 and then put them to a new file, file2. I was able to do this without any problem with the following statement: cat file1 | awk '{print $2}' | awk '!x++' > file2Here is what I can't... (2 Replies)
Discussion started by: jontjioe
2 Replies

8. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

9. 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

10. Shell Programming and Scripting

Merging files with AWK filtering and counting lines

Hi there, I have a couple of files I need to merge. I can do a simple merge by concatenating them into one larger file. But then I need to filter the file to get a desired result. The output looks like this: TRNH 0000000010941 ORDH OADR OADR ORDL ENDT 1116399 000000003... (2 Replies)
Discussion started by: Meert
2 Replies
Login or Register to Ask a Question