LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-20-2012, 05:16 AM   #1
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Rep: Reputation: Disabled
Parsing numbers in awk


i need code in awk to search about nubers start by "9678"
962786541654 *
962796556256
962777655155
962786565165 *
 
Old 12-20-2012, 05:21 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
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?

Last edited by sycamorex; 12-20-2012 at 05:28 AM. Reason: Yes, awk is the easiest tool here.
 
Old 12-20-2012, 06:02 AM   #3
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
!!

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
 
Old 12-20-2012, 06:22 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Quote:
Originally Posted by eyadgh View Post
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.
 
Old 12-20-2012, 06:30 AM   #5
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
!!!

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
 
Old 12-20-2012, 06:45 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
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.
 
Old 12-20-2012, 06:48 AM   #7
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
thaaaaaaaaaaaaaaanks

thank you so much
 
Old 12-20-2012, 06:52 AM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
You're welcome. If your problem has been solved, please mark the thread as solved in the thread tools.
 
Old 12-20-2012, 07:00 AM   #9
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
wait until test code
 
Old 12-20-2012, 07:01 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
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! )

Last edited by David the H.; 12-20-2012 at 07:03 AM.
 
2 members found this post helpful.
Old 12-22-2012, 08:43 AM   #11
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-22-2012, 09:30 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,007

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
Quote:
Originally Posted by eyadgh View Post
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??
 
Old 12-22-2012, 09:48 AM   #13
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-22-2012, 10:36 AM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
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.

Last edited by David the H.; 12-22-2012 at 10:43 AM. Reason: minor
 
2 members found this post helpful.
Old 12-22-2012, 02:54 PM   #15
eyadgh
Member
 
Registered: Dec 2012
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 02:20 PM
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 09:51 AM
Awk function for parsing files nev2010 Red Hat 4 11-18-2010 08:28 AM
awk question - parsing in a pipeline nysalsa Programming 2 10-06-2009 07:33 PM
Parsing using awk tungaw2001 Programming 2 03-27-2007 05:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration