Get information from one files, based on data from other file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Get information from one files, based on data from other file
# 1  
Old 07-02-2018
Get information from one files, based on data from other file

Hello.

I am trying to get some info from log file.

I have fileA , which contains all the country prefixes (the file contains one column and "n" rows ).

And i have fileB, which contains huge data of phone numbers (the file contains one column and "n" rows).

What i want to do is, to count how many phone calls we have made to certain country (prefix).
For example :

If fileA contains:
Code:
44
39
359

And fileB contains:
Code:
44858475648
44645645645
3943534534

The output should be:
Code:
                                44 - 2 times
                                39 - 1 time

Any help would be appreciate.

Regards,
DF

Last edited by vbe; 07-02-2018 at 11:26 AM.. Reason: code tags please
# 2  
Old 07-02-2018
The lazy way if you just have to run it once.
Code:
while read cc
do
   echo $cc $(grep -c "^$cc" fileb)
done <filea

If you have to run this frequently, or the number of records in filea is large then you may want to first sort fileb and create a "rolling totals" type program.
These 2 Users Gave Thanks to jgt For This Post:
# 3  
Old 07-02-2018
or:
Code:
 awk 'FNR==NR {a[$1];next} {for(i in a) if ($0 ~ ("^"i)) a[i]++}END {for (i in a) printf("%d - %d times\n", i, a[i])}' fileA fileB

These 2 Users Gave Thanks to vgersh99 For This Post:
# 4  
Old 07-02-2018
Supress showing the zero matches:
Code:
while read cc
do
   c=$(grep -c "^$cc" fileb) &&
   echo "$cc - $c times"
done <filea

Code:
...
 END {for (i in a) if (a[i]) printf("%d - %d times\n", i, a[i])}
' fileA fileB

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-02-2018
Single grep approach:
Code:
sed 's/^/^/' filea | grep  -of- fileb | sort | uniq -c
      1 39
      2 44

You can drop the sort if fileb is already sorted.
These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 07-03-2018
Thanks a lot guys.

With sed and awk examples, it worked like a charm.
I couldnt do it with "while do" , but i will keep trying.

Once again, thanks to everyone.
# 7  
Old 07-03-2018
Post the "while do" that you are struggling with for analysis / help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

3. UNIX for Beginners Questions & Answers

Create file based on data from two other files

I have looked through several threads regarding merging files with awk and attempted using join however have been unsuccessful likely as I do not fully understand awk. What I am attempting is to take a csv file which could be between 1 and 15,000 lines with 5 colums and another csv file that will... (4 Replies)
Discussion started by: cdubu2
4 Replies

4. Shell Programming and Scripting

Pulling information from a data file by date

awk -v now="$(date +%s)" -v tDiff="${USERMINUTES}" ' BEGIN { FS="=" if (!now) now=systime() if (!tDiff) tDiff=60*60 p=1 } /{/ {rec=$0;p=1;next} /}/ && rec && p {print rec ORS $0;next} $1=="entry_time" { if (now-$2>tDiff)p=0 } {rec=rec ORS $0}'... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. UNIX for Dummies Questions & Answers

Obtaining File information based on String Search

Is there a single Command in Unix to get the following Information when searching for files containing one or more strings in a Unix Directory (including sub directories within it) : 1) Complete filename ( path and filename) 2) Owner of the file 3) Size of the file 4) Last Modified date... (3 Replies)
Discussion started by: pchegoor
3 Replies

6. Shell Programming and Scripting

Appending information from 2nd file into 1st based on intervals

Hi, I am trying to gather information from the second file and append it to the first file. input HWUSI-EAS000_29:1:100:10000:11479#0/1 + chr5 14458050 ATTGGCTGAGGTCCTACTAGTTGTGATGTGTAAGTGT HHHHHHGDGGEDGGGDGCGEDDEFFFAGE 0 second file:... (14 Replies)
Discussion started by: Diya123
14 Replies

7. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

8. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

9. Shell Programming and Scripting

How to extract information from two files with data range

Hi, I want to make a query about extracting data from two files that both have data ranges. the data that i want to extract; when there is matching between file1 column 2 is equal to file2 column2 , and file1 column 3 and column 4 is within the range of file2 columns 3 and 4. I would like rows... (1 Reply)
Discussion started by: houkto
1 Replies

10. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies
Login or Register to Ask a Question