Visit Our UNIX and Linux User Community


Count the number of string occurrences to display 0 entries in output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the number of string occurrences to display 0 entries in output
# 1  
Old 08-31-2017
Count the number of string occurrences to display 0 entries in output

Hello Friends,

Can somebody assist an issue I am having? I have a separate file with a list of account ids

Code:
XXX200B02Y01
XXX200B03Y01
XXX200B05Y01
XXX200B07Y01
XXX200B08Y01

I call the file, and run an egrep against a directory and logfiles

Code:
AccountID=$(cat /home/resource/frodo/tmp/al/ETakers)

/raid/test/`date +%Y/%m/%d`/test/orders/* | /bin/egrep "$AccountID" |grep SUBMITTED | sed 's/.*UserId:\(.*\),Id.*/\1/g' |sort |uniq -c >> /home/resource/frodo/tmp/al/scripts/XXXordertestCOPY.txt

Now getting the correct count is not the issue.

Code:
40    XXX200B02Y01
58    XXX200B03Y01
953    XXX200B05Y01
737    XXX200B07Y01
1702    XXX200B10Y1
1028    XXX200B30Y01
1557    XXX200C02
40    XXX200D02Y01
58    XXX200D03Y01
952    XXX200D05Y01
735    XXX200D07Y01
1694    XXX200D10Y01
1026    XXX200D30Y01
939    XXX200E05Y01
753    XXX200E07Y01
1130    XXX200E10Y01
1029    XXX200E30Y01
1020    XXX201B01
837    XXX201B02
965    XXX201B03
415    XXX202B01
415    XXX202B02
415    XXX202B03
307    XXX203B02
642    XXX203B03
309    XXX203C01
518    XXX203C02

The issue, i'm having is getting the account ids to not match to be printed as well. ex.

Code:
0 XXX200B07Y01
0 XXX200B08Y01
642    XXX203B03
309    XXX203C01
518    XXX203C02

do i need to add some counter and set to 0 somewhere?

your assistance is greatly appreciated.

Al

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-31-2017 at 05:13 PM.. Reason: Added CODE tags.
# 2  
Old 08-31-2017
Not understanding what you're doing in your code, I guess that this might come close to what you want:
Code:
awk 'NR == FNR {S[$1]; next} $1 in S {S[$1]++} {T[$1]} END {for (s in S) print S[s], s; for (t in T) print 0, t}' file1 file2
1 XXX200B07Y01
 XXX200B08Y01
7 XXX200B02Y01
6 XXX200B03Y01
3 XXX200B05Y01
0 XXX200E07Y01
0 XXX200D07Y01
0 XXX200B07Y01
0 XXX202B01
0 XXX200B10Y1
0 XXX202B02
0 XXX202B03
0 XXX200E10Y01
0 XXX200D10Y01
0 XXX200E30Y01
0 XXX200D30Y01
0 XXX200B30Y01
0 XXX203B02
0 XXX203B03
0 XXX200B02Y01
0 XXX200D02Y01
0 XXX203C01
0 XXX203C02
0 XXX200B03Y01
0 XXX200D03Y01
0 XXX200E05Y01
0 XXX200B05Y01
0 XXX200D05Y01
0 XXX200C02
0 XXX201B01
0 XXX201B02
0 XXX201B03

# 3  
Old 09-01-2017
You have not shown a sample of your input files in orders/.
Your problem is that grep only lists the matches, i.e. 1 time or higher.
awk is certainly a better tool for this.
Guessing you omitted a cat, and by your output it looks like sed cuts out the IDs
Code:
sed -n '/SUBMITTED/ s/.*UserId:\(.*\),Id.*/\1/p'  /raid/test/`date +%Y/%m/%d`/test/orders/* |
awk '
  { if (FILENAME!="-") { S[$1]=0 } else { S[$1]++ } }
  END { for (s in S) print S[s], s }
' /home/resource/frodo/tmp/al/ETakers -


Previous Thread | Next Thread
Test Your Knowledge in Computers #258
Difficulty: Medium
Iannis Xenakis wrote programs in the FORTRAN language that generated numeric data that he transcribed into scores to be played by traditional musical instruments.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Extract count of string in all files and display on date wise

Hi All, hope you all are doing well! I kindly ask you for shell scripting help, here is the description: I have huge number of files shown below on date wise, which contains different strings(numbers you can say) including 505001 and 602001. ... (14 Replies)
Discussion started by: VasuKukkapalli
14 Replies

3. Shell Programming and Scripting

How to count number of characters of wc -l output?

I want count number of characters / find the length of the 'wc -l' output This is the command bash-3.2$ gzcat /home/sid/file1.dat |wc -l 830752 So final out I want is 6 i.e lenght of 830752 I tried with awk bash-3.2$ gzcat /home/sid/file1.dat |wc -l | awk '{print length ($0)... (3 Replies)
Discussion started by: sidnow
3 Replies

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

5. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Discussion started by: sheelsadan
5 Replies

6. Shell Programming and Scripting

Help with Unix and Awk to count number of occurrences

Hi, I have a file (movies.sh), this file contains list of movies such as I want to redirect the movies from movies.sh to file_to_process to allow me process the file with out losing anything. I have tried Movies.sh >> file_to_process But I want to add the row number to the data... (2 Replies)
Discussion started by: INHF
2 Replies

7. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

8. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

9. Programming

output the letters of the alphabet with the number of occurrences

hi, I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!... (10 Replies)
Discussion started by: svd
10 Replies

10. UNIX for Dummies Questions & Answers

How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3" I want to know how many "|" are in $var. When I say echo $var | grep -c '|' I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
Discussion started by: jingi1234
4 Replies

Featured Tech Videos