LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Parsing numbers in awk (https://www.linuxquestions.org/questions/linux-newbie-8/parsing-numbers-in-awk-4175442286/)

eyadgh 12-20-2012 05:16 AM

Parsing numbers in awk
 
i need code in awk to search about nubers start by "9678"
962786541654 *
962796556256
962777655155
962786565165 *

sycamorex 12-20-2012 05:21 AM

Hi and welcome to LQ.

So how can we help you? Where are you stuck in your code? Can you show it to us so that we can guide you?

edit: I assume you meant "96278", didn't you?

eyadgh 12-20-2012 06:02 AM

!!
 
cat file.txt :

96278-6541654 call 100sec 962791235165
96279-6556256 call 110sec 962793165133
96277-7655155 call 600sec 962795313245
96278-6565165 call 300sec 962798531759
96277-2313231 call 410sec 962793541867

i need code awk print sum of time of calls doing by :
96278- alone
96277- alone
96279-alone

thank you

sycamorex 12-20-2012 06:22 AM

Quote:

Originally Posted by eyadgh (Post 4853595)
i need code awk print sum of time of calls doing by :
96278- alone
96277- alone
96279-alone

thank you

I'm confused. First you wrote that you only need numbers starting with "9678" (I assume you meant 96278). Now you seem to need to extract all numbers up to the dash (-). Can you clarify it?

Additionally, do you just need to extract/print them or calculate the total of them as well. Please clarify.

Furthermore, what have you tried? Can you show us your code? It'd be nice of you to show that you've put some effort and at least tried to come up with a solution. Expecting us to do all the work for you might be considered rude and it won't really help you in the long run.

eyadgh 12-20-2012 06:30 AM

!!!
 
my friend this is a numbers of mobiles doing calls to other numbers :
96278-****** acertin operator comanpy
96279-****** another operator company
96277-****** another operator comapny

just i need to calculate sum of calls in mintues but each operator alone:
hint : no need for dash example : 96278565794 , 962775654969, 962792547963

sycamorex 12-20-2012 06:45 AM

Here's your Christmas gift:

Code:

sed -n '/^96278/ s/sec//p' file.txt | awk '{x += $3} END {print "Operator: "$1,"Total: " x" seconds"}'
If you want to change it for other companies you need to change the /^96278/ bit, or even better write a script that will loop over possible operator prefixes. I don't know the range of numbers for your operators.

eyadgh 12-20-2012 06:48 AM

thaaaaaaaaaaaaaaanks
 
thank you so much :)

sycamorex 12-20-2012 06:52 AM

You're welcome. If your problem has been solved, please mark the thread as solved in the thread tools.

eyadgh 12-20-2012 07:00 AM

wait until test code

David the H. 12-20-2012 07:01 AM

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

It shouldn't take more than an hour for you to read through the first link at least and find what you want. Learning how to do it yourself will do you better in the long run, and may even be faster than waiting for someone else to give you the answer.

If you still have questions after working on it a bit, please post what you've tried and we'll be happy to help you out.


PS. Does it have to be awk? Because if you just want to print out lines that contain a certain pattern, grep is generally the tool to use.

( edit: Sheesh, a whole conversation appeared in the time it took me to write this! )

eyadgh 12-22-2012 08:43 AM

I have this file :
00962798578964 600 00962785985764
00962798578964 110 00962786897532
00962798578964 61 00962776898523
00962798578964 111 00962795225325
00962798578964 45 00962777965862
00962798578964 55 00962799548155


- $1 : number of mobile doing calls to another numbers
- $2 : time of calls in secondes.
- $3 : number of reciver call.


I need code do this :
Print time of calls in mintues as this :
600/60 = 60
110/60= 2
61/60=2
111/60=2
45/60=1
55/60=1
*hint : if time of call < 60 sec then time= 1 mintue

*hint in this file we have three companys (in $3) numbers:
-0096278*******
-0096277*******
-0096279*******

*I want to print sum of time (in mintue) for each company alone :
Example :
600/60 + 110/60 = 12 min
Example :
61/60 + 45/60 = 3 min
Exactly I want to print :
12 min
3 min
3 min

TB0ne 12-22-2012 09:30 AM

Quote:

Originally Posted by eyadgh (Post 4854956)
I have this file :
00962798578964 600 00962785985764
00962798578964 110 00962786897532
00962798578964 61 00962776898523
00962798578964 111 00962795225325
00962798578964 45 00962777965862
00962798578964 55 00962799548155


- $1 : number of mobile doing calls to another numbers
- $2 : time of calls in secondes.
- $3 : number of reciver call.


I need code do this :
Print time of calls in mintues as this :
600/60 = 60
110/60= 2
61/60=2
111/60=2
45/60=1
55/60=1
*hint : if time of call < 60 sec then time= 1 mintue

*hint in this file we have three companys (in $3) numbers:
-0096278*******
-0096277*******
-0096279*******

*I want to print sum of time (in mintue) for each company alone :
Example :
600/60 + 110/60 = 12 min
Example :
61/60 + 45/60 = 3 min
Exactly I want to print :
12 min
3 min
3 min

Ok...so post what you've written/tried so far, and we can help. DavidtheH gave you links that show you how to do it on your own...have you read/tried any of them yet??

eyadgh 12-22-2012 09:48 AM

i'm junior in awk but am need just this code
awk -F" " '$3~/^96278[0-9/|/^96277[0-9]/|/^96279[0-9]/{ if (!($2%60==0)) {printf "%2d\n",$2/60+1} else print $2/60;}}'

thats all

David the H. 12-22-2012 10:36 AM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.



It would be nice if you showed us that you are attempting to solve it yourself. What have you come up with so far? Have you checked out any of the links I gave you?


In any case, what we need to do here is create an array entry for each company prefix you encounter, and add the $2 call time to its total each time you encounter it. Then at the end we print out the results.


Now, you haven't explained exactly what part of the number is company prefix, but I assume it's the first 7 digits. So we'll use the substr function to extract it.

Also, your calculations are a bit more complex than they seem at first glance. It appears that each call has to be calculated separately, and any fractions of a minute have to be counted as a full minute.

Code:

awk '{ comp[substr($3,1,7)] += ( int( $2/60 ) + $2%60 ? 1 : 0 ) } END{ for (i in comp){ printf "%s: %d min\n" , i , comp[i] } }'

The first time awk finds a company number in $3, a new entry for it will be added to the comp array, and its value set to the call total. Every subsequent time the same company matches, it will have that $2 value added to this.

The call total is calculated by first dividing $2 by 60, and taking the integer value only. Then a "%" (modulo operator) calculation is done on the same number to give us the remainder, and a ternary conditional operator is used to decide whether to add one or zero to it.

Finally, in the END section we simply loop over the array and print the totals.

Note that array loops generally come out unsorted, so you'll have to pipe the output through sort to clean up the order if you need it. Or if you're using gawk then you can use one of it's built-in sorting features.

eyadgh 12-22-2012 02:54 PM

my friend it didnt work !!

1-yes every prfix of each company is 7 digit
2- we must chek(dividing & chek) time of each call (in same company ) then sum it
3-print sum of time of each company alone

you are great programmer
again thanks


All times are GMT -5. The time now is 12:44 PM.