I'm trying to find the number of times a particular email ID repeats. Piping uniq -c
with the output I get 420 aol.com
but I need aol.com 420
how to I do this because uniq -c
applies as a prefix count
Asked
Active
Viewed 2,253 times
0

anush95
- 71
- 1
- 1
- 5
-
add a sample input with emails and expected output... your question as such doesn't have enough detail to suggest a solution.. see also https://stackoverflow.com/help/mcve – Sundeep Oct 21 '16 at 02:09
-
@Sundeep the input is abc123@aol.com and current output : 1 aol.com ; meaning 1 aol.com email was found ; The output I need is : aol.com 1 – anush95 Oct 21 '16 at 02:09
-
oh ok, that is easy to do and you've got an answer already :) see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Sundeep Oct 21 '16 at 02:17
2 Answers
4
You can pipe the output to awk
:
string='420 aol.com'
echo "$string" | awk '{print $2,$1}'
aol.com 420
By default, awk
separates each field by spaces.
420
is the 1st field$1
aol.com
is the 2nd field$2
.
This simply tells awk
to print each of these field in reverse order. {print $2,$1}

I0_ol
- 1,054
- 1
- 14
- 28
-
Does this work even if it's a list 420 aol.com 123 yahoo.com 11 gmail.com would this give aol.com 420 yahoo.com 123 gmail.com 11 – anush95 Oct 21 '16 at 02:20
-
2sure it will. the output of `uniq` will display each result set in new line. `awk` would just swap `1st` and `2nd` columns of this output. – Utsav Oct 21 '16 at 02:26
0
If you have a list of email addresses such as:
$ cat emails.txt
1@aol.com
2@yahoo.com
3@aol.com
and you need the count of their domains, using awk:
$ awk -F@ '{a[$2]++} END {for(i in a) print i, a[i]}' emails.txt
aol.com 2
yahoo.com 1
The records are outputed in no particular order. If using Gnu awk, records can be ordered using PROCINFO["sorted_in"]="@val_num_asc";
(see here of options) in the beginning of the BEGIN
block.

James Brown
- 36,089
- 7
- 43
- 59