File manipulation?
1 Attachment(s)
Dear All
From my previous posts, I learned on how to modify my text file to filter out undesired ones. I need to know how to find the number of occurances of distinct records in my text file. Please find attached my text file. Can you please show me the power of my Linux on file manipulation issues? Actually, I want to find the number of occurances of distinct CallId in my logfile. |
From what I understood !!
This will count number of uniquer CallIs Code:
cut -d" " -f2 <log_filename> | sort -u | wc -l |
Code:
awk 'END{print NR}' logfile4.txt |
@ grail
This will not give you the unique count, as I understood OP want count of unique Callids |
I am getting two different results from the following two codes :
#awk '{print $2}' logfile4 | sort -u | wc -l #cut -d " " -f2 logfile4 | sort -u | wc -l In your opinion , which one is correct? |
What is the difference ?
|
The correct answer is the one with awk
ie. ~~>"awk '{print $2}' logfile4 | sort -u | wc -l"<~~ The manner in which cut is used above fails for the below given lines where the second field starts after "two" blank spaces after the first field ends; whereas awk simply ignores the consecutive blank spaces while separating the columns. CallId 400 State TK bt 2 bt 0 Tr (2 0x0d) E (3 0 1) Tr (0 0 2) CallId 3 State TK bt 7 bt 2 Tr (13 0x0f) E (4 1 11) Tr (0 2 0) CallId 3 State TK bt 7 bt 2 Tr (13 0x0f) E (4 1 11) Tr (0 2 0) CallId 3 State TK bt 7 bt 2 Tr (13 0x0f) E (4 1 11) Tr (0 2 0) |
Quote:
Code:
c@CW8:~$ echo 'CallId 9 State TK' | cut -d " " -f2 |
Oops...
The extra space is not visible in the post, although it is present. Just vi the file and go to line number 1393 and u will be able to see that. |
Thank you very much. So I will base my calculations on the result of 'awk' output.
|
Thanks PMP, missed that bit :( This should do:
Code:
awk '!_[$2]++{uniq++}END{print uniq}' logfile4.txt |
I'm confused about what the OP wants.
|
Code:
awk '{print $2}' logfile.txt | sort | uniq | wc -l |
Code:
cut --delimiter=' ' --fields=2 | sort --unique | wc --lines |
All times are GMT -5. The time now is 10:32 AM. |