Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to create a filter that will read mobile phone numbers in a calendar, filter only the information I need and output it in a file.
So basically I need it to find the numbers that
- start with 94, 95, 96, 97 and 99
- are 8 digits long
- ignore any spaces in the number
- ignore country prefix (+357 or 00357)
Basically something more robust than what I currently have. Because now it picks up anything that contains (not starts with) 94, 95, 96, 97 and 99 and doesn't ignore country code.
basically you need to use only one tool, for example grep, awk, perl, python and construct a single regexp which will do the job.
Would be nice to see an example for input and output. How to know what is a correct country prefix?
The problem is that the person entering the phone numbers in the calendar might add the country prefix (which is always +357 or 00357 or simply 357) with or without spaces, or the phone number with or without spaces, or anything they can imagine.
What I need to do, is do my best to figure out if the information entered is a mobile phone number or not.
All mobile phone numbers start with 94, 95, 96, 97 and 99 so thats a good start. After that there's an additional 6 digits, without any spaces, but that doesn't stop the person entering the information from adding spaces. So I need to remove them. And I can simply ignore the country prefix, because I don't use it when sending an SMS. (thats the purpose of this script.)
ok. what I see: you can use a single awk to:
1. get/parse date, time
2. put the end of the line from column 32(?) into a variable
3. remove spaces, country prefix in this variable
4. check ^9[45679] and skip line if not needed
5. print the result in the required format.
you can do the same in awk/perl/python/whatever or even in plain bash, choose your preferred language.
avoid those pipe chains if possible.
I'd second the use of AWK. The above steps would cover it, but since you have tabs separating the fields things are much easier. You can set the FS (input field separator) pattern to be just a plain tab.
Code:
awk -F "\t" ...
awk ... FS="\t"
Then it is a matter of making a pattern to select on the fifth while manipulating the contents of the first and fifth fields and printing the results.
Thanks for the suggestions. The code I posted took me about a month to figure out, and its not really working as I wanted it to. Any chance of helping me out with some code snippets? Not looking for ready-made stuff, but it will take me a long time to figure out your suggestions.
{ number = substr($0, 32) # take the last field
number = gensub("regexp", "", number) # remove country
number = gensub("regexp", "", number) # remove spaces and whatever
if match (number, "^9[456789]") # check first two chars
}
I interpreted the --tsv to mean tab-separated-values. However, what you posted in #3 above has other than tabs and newlines for field and record separators. That will throw a wrench into the works until addressed.
Nevermind, I seem to have gotten mixed on things there.
It's tempting to try to use a positive lookbehind pattern, even if the variable width ones are still experimental:
Code:
#!/usr/bin/perl
use strict;
use warnings;
# read stdin until no more input
while (<>) {
# split the special variable for default input into named variables
my ($date1, $time1, $date2, $time2, $phone) = split(/\t/, $_);
next unless ($phone);
$phone =~ s/\s//g; # ignore any spaces
$phone =~ s/\+357/00357/; # standardize country code
# swap the day and the year around
$date1 = join('-',reverse(split(/-/, $date1)));
# use positive lookbehind pattern to
# find an 8-digit number starting with 94 - 99
# if it is by itself or if it follows the right country code
if ($phone =~ m/(?<=^00357|^)9[4-9][0-9]{6}$/) {
print qq($date1\t$time1\t$phone\n);
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.