Visit Our UNIX and Linux User Community


awk script to count characters in file 1 in file 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to count characters in file 1 in file 2
# 1  
Old 04-17-2013
awk script to count characters in file 1 in file 2

I need a scripting AWK to compare 2 files.

file 1 and 2 are list of keywords
1 is
Code:
a
b
c
d

2 is
Code:
aa
aaa
b
bb
ccc
d

I want the AWK script to give us the number of times every keyword in file 1 occurs in file 2.
output should be
Code:
a 2
b 2
c 1
d 1


Last edited by Franklin52; 04-17-2013 at 06:09 AM.. Reason: Please use code tags
# 2  
Old 04-17-2013
Please use code tags as required by forum rules!

Code:
awk     'NR==FNR {TMP[$1];next}
                 {for (i in TMP) if (match ($0, i)) TMP[i]++}
         END     {for (i in TMP) print i, TMP[i]}
        ' file1 file2
a 2
b 2
c 1
d 1

# 3  
Old 04-17-2013
Quote:
Originally Posted by anhtt
I need a scripting AWK to compare 2 files.

file 1 and 2 are list of keywords
1 is
a
b
c
d

2 is
aa
aaa
b
bb
ccc
d

I want the AWK script to give us the number of times every keyword in file 1 occurs in file 2.
output should be
a 2
b 2
c 1
d 1

Yes you need that, you should also post what have you tried so far with respect to the problem.
# 4  
Old 04-17-2013
Code:
$ cat temp.sh
while read pattern; do
  echo -n "$pattern "
  grep -c $pattern file2
done < file1

Code:
$ ./temp.sh
a 2
b 2
c 1
d 1
f 0

I added additional "f" line to file1 for better test.
# 5  
Old 04-17-2013
Take some care when using match ($0, i)
Not sure what the intention of this counting is, but if the code have a line like abb it would be counted both as a and as b
# 6  
Old 04-17-2013
Thanks for all friends

I have a big text file that need to process.
As I know, awk is the best tool to process text file. That why I'm looking for someone help me. Because for a long time, about 5 years, I haven't use awk. So I cann't remember any more.

Hi. RudiC

I've done the test with your script. But I don't know why the result have 6 on the top of output results.
6
a 2
b 2
c 1
d 1

Can you explain to me? And how can I delete it in output result? Thanks
# 7  
Old 04-17-2013
If you just want to delete the first line, "tail -n +2"
Code:
$ seq 3
1
2
3

Code:
$ seq 3 | tail -n +2
2
3


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to detect specific string in a log file and count it

Hello, can someone guide me on this? I don't know what is the best approach, (awk script, shell script) I am using RedHat Linux version 6.5. There is a third party application deployed on that server. This app by default generates 5 log files and each file is 20MB. These log rollover... (5 Replies)
Discussion started by: ktisbest
5 Replies

2. Shell Programming and Scripting

Help with listing file name containing particular text and count of lines with 10 characters.

Hi, I've 2 queries. I need to list files which doesn't contain a particular text in the content. For example say, I need to list files which doesn't contain string "abc" from all files ending with *.bad. How can I do that? Also, I want to display number of lines in a file which has atleast... (2 Replies)
Discussion started by: Gangadhar Reddy
2 Replies

3. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

4. Shell Programming and Scripting

Count characters in a csv file and add an word.

Hello, I want to add a sentence to "post column" those who are only less than 30 characters.Thank you very much for your help. "category","title","post" "Z","Zoo","test 54325 test 45363mc." "Z","Zen","rs2w3rsj 2d342dg 2d3s4f23 d23423s23h 2s34s2423g ds232d34 2342." "Z","Zet","test4444... (3 Replies)
Discussion started by: hoo
3 Replies

5. Shell Programming and Scripting

Checking a pattern in file and the count of characters

I am having a zipped file which has the following URL contents - 98.70.217.222 - - "GET /liveupdate-aka.symantec.com/1340071490jtun_nav2k8enn09m25.m25?h=abcdefgh HTTP/1.1" 200 159229484 "-" "hBU1OhDsPXknMepDBJNScBj4BQcmUz5TwAAAAA" "-" In this line here is we only need to consider the... (4 Replies)
Discussion started by: Naks_Sh10
4 Replies

6. Shell Programming and Scripting

script to tail file; problem with awk and special characters

Trying to use code that I found to send only new lines out of a log file by doing: while :; do temp=$(tail -1 logfile.out) awk "/$last/{p=1}p" logfile.out #pipe this to log analyzer program last="$temp" sleep 10 done Script works fine when logfile is basic text, but when it contains... (2 Replies)
Discussion started by: moo72moo
2 Replies

7. Shell Programming and Scripting

Unix Script file to Append Characters before rows in file.

Hi Experts, I am working on HP-UX. I am new to shell scripting. I would like to have a shell script which will prefix: 1. "H|" before first row of my file and, 2. "T" for all other rows of the file. For Example - File before running the script 20100430|4123451810|218.50|TC 20100430 ... (4 Replies)
Discussion started by: phani333
4 Replies

8. Shell Programming and Scripting

Include special system characters in file count

Hi, I have a script that checks the length of each record/line in file - This seems to be working when there are no special systems character that are invisible or hidden. awk -v file=$file '{ if (filename==file) { k+=$5 if (length() <= 10 ){print size length(), "bytes " k} }... (2 Replies)
Discussion started by: asemota
2 Replies

9. Shell Programming and Scripting

awk script to count percentage from log file

Hi, I have a log like this : actually i want to get the log like this : where % can get from : 100 * pmTotNoRrcConnectReqSucc / pmTotNoRrcConnectReq Thanks in advance.. :) (8 Replies)
Discussion started by: justbow
8 Replies

10. Shell Programming and Scripting

how to count characters by line of file ?

Hello, Member or professional need help how to count characters by line of file Example of the file is here cdr20080817164322811681txt cdr20080817164322811txt cdr20080817164322811683txt cdr20080817164322811684txt I want to count the characters by line of file . The output that I... (4 Replies)
Discussion started by: ooilinlove
4 Replies

Featured Tech Videos