count numbers of matching rows and replace its value in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count numbers of matching rows and replace its value in another file
# 1  
Old 11-28-2009
count numbers of matching rows and replace its value in another file

Hello all,
can you help me in this problem, assume We have two txt file (file_1 and file_3) one is file_1 contains the data:

a 0
b 1
c 3
a 7
b 4
c 5
b 8
d 6
.
.
.
.
and I need to count the lines with the matching data (a,b,..) and print in new file called file_2 such as the output:

a 2
b 3
c 2
d 1

the other file file_3 contains the data:

a A
b B
c C
d D
.
.
.
now i want two merge the two file to obtain the file format as shown:

A 2
B 3
C 2
D 1
# 2  
Old 11-28-2009
There's no need to use the second file:

Code:
awk 'NR==FNR{a[$1]++$2;next} a[$1]{print $2 FS a[$1]}' file1 file3

# 3  
Old 11-28-2009
Code:
$ awk 'NR==FNR{a[$1]++$2;next}{print $2,a[$1]}' file1 file3
A 2
B 3
C 2
D 1

# 4  
Old 12-04-2009
Hello,
I try both code but it doesn't work in solaris (give wrong output)
# 5  
Old 12-05-2009
Quote:
Originally Posted by GoldenFalcon10
Hello,
I try both code but it doesn't work in solaris (give wrong output)
in Solaris, use nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

2. Shell Programming and Scripting

Replace a character of specified column(s) of all rows in a file

Hi - I have a file "file1" of below format. Its a comma seperated file. Note that each string is enclosed in double quotes. "abc","-0.15","10,000.00","IJK" "xyz","1,000.01","1,000,000.50","OPR" I want the result as: "abc","-0.15","10000.00","IJK" "xyz","1,000.01","1000000.50","OPR" I... (8 Replies)
Discussion started by: njny
8 Replies

3. UNIX for Dummies Questions & Answers

Count rows in .dat file

Could you please tell e how to find the total number of rows in a .dat file. edit by bakunin: this is exactly why in "advanced and expert" section?? I transfer this thread to "Unix for Dummies Questions and Answers" (2 Replies)
Discussion started by: Deeptanshu
2 Replies

4. UNIX for Dummies Questions & Answers

Count of rows starting with 01 in a file

Hi I am very new to unix and please help me in solving the below problem I have a file with 50000+ rows. Each row(line) start with 01 or 02 or 03. Now i have to get the count of rows starting with 01 only, Thanks in advance (3 Replies)
Discussion started by: nmakkena
3 Replies

5. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

Delete rows based on line numbers in a file

I have to find the number of rows in a file and delete those many rows in another file. For example, if I have 3 rows in a file A, i have to delete first 3 rows in anothe file B, I have the code, it works as standalone, when I merge this with m application (c with unix), it doesnt work. ... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

7. Shell Programming and Scripting

Grouping data numbers in a text file into prescribed intervals and count

I have a text file that contains numbers (listed from the smallest to the largest). For ex. 34 817 1145 1645 1759 1761 3368 3529 4311 4681 5187 5193 5199 5417 5682 . . (5 Replies)
Discussion started by: Lucky Ali
5 Replies

8. Shell Programming and Scripting

How to replace rows from...to in a file?

Here is a description what i need: Document1: start... aaa bbb ccc ...end ======================= Document2: start... <paste the copied lines here> ...end All rows of document1 between "start...end" should be copied into the empty section "start...end" of document2. The... (3 Replies)
Discussion started by: smitty11
3 Replies

9. AIX

How to replace many numbers with one number in a file

How to replace many numbers with one number in a file. Many numbers like 444565,454678,443298,etc. i want to replace these with one number (300).Please halp me out. (2 Replies)
Discussion started by: vpandey
2 Replies

10. Shell Programming and Scripting

to replace one character by numbers in a file

suppose u have a file aas P-H 123 gdg O-U 223 hdy I-Y 12 fgd K-O 333 ssa L-P 32 output shud be like that aas P123H gdg O223U hdy I12Y fgd K333O ssa L32P thanks (7 Replies)
Discussion started by: cdfd123
7 Replies
Login or Register to Ask a Question