Reputation: 1875
I am trying to write a regex that will find in CVS(Coma Separate Values) file bunch of phone numbers.
Catch is I am interested only in phone numbers in particular column(as an only after particular amount of comas). Bellow I have regex that will do that and it works fine per Javascript standard.
(?:^([^^]*\,){3}[^^]*)\d{3}-\d{3}-\d{4}
I am actually working in Bash and using sed, grep but I cannot even find what Regex standard does grep, and sed use?
Here is sample text.
note that right now I am using '^' instead of ',' to keep values separated, because users included comas in the value.)
THIS IS NOT THE ACTUAL DATA, IT IS SCRAMBLED TO PRESERVE PEOPLE'S PRIVACY
28434658^17 Three^2013-09-19T19:57:23Z^80 W 54th St, Penthouse & 4th Fl, NY, 10018s212-409-1641^^Mary Szyb 347-340-1918^2 x week Thur 2.5hrs & Sat 4 hrs
28937693^356 West 36th street^2013-09-19T18:17:57Z^356 West 36th street, suite 706sNew York New York 10018^null^null^on call:
29219313^333 rector pl^2013-10-07T17:11:36Z^333 Rector Place 248-469-5859^^Jose Hernandez^2 x week Wed & Fri
28854346^50 Can^2013-09-23T13:10:54Z^152 East 28th Street, 7th Floor, NY, 10018s917-932-3962s646-710-4170^155 W 24rd St 3rd FL^null^Swlvia Smith347-933-6630sIrena Brown 347-991-1346s5 x week Mon-Fri
28434698^4Eleven^2013-09-19T19:57:23Z^112 West 28th Street, 3th Fl,sNY, 10018s917-922-3862s646-710-4170^^null^null
Let me also clarify one thing correct output would be:
212-409-1641
248-469-5859
917-932-3962
646-710-4170
917-922-3862
646-710-4170
Because these are the only phone numbers in column 4
Upvotes: 0
Views: 96
Reputation: 1875
I am posting the regex that ended doing the job:
([0-9]{3}-[0-9]{3}-[0-9]{4})(?=[^^]*(\^[^^]*){3}$)
thank you everyone for the helpful input
I guess my lesson from that problem is if one solution does not work try to work from different angle, in this case count the columns from the back.
Upvotes: 0
Reputation: 70722
The following should work for you.
grep -Po '(\d{3}-){2}\d{4}' file.csv
UPDATE:
After replacing ^
with comma's as they are in you actual data..
28434658,17 Three,2013-09-19T19:57:23Z,80 W 54th St, Penthouse & 4th Fl, NY, 10018s212-409-1641,Mary Szyb 347-340-1918,2 x week Thur 2.5hrs & Sat 4 hrs
28937693,356 West 36th street,2013-09-19T18:17:57Z,356 West 36th street, suite 706sNew York New York 10018,null,null,on call:
29219313,333 rector pl,2013-10-07T17:11:36Z,333 Rector Place 248-469-5859,Jose Hernandez,2 x week Wed & Fri
28854346,50 Can,2013-09-23T13:10:54Z,152 East 28th Street, 7th Floor, NY, 10018s917-932-3962s646-710-4170,155 W24rd St 3rd FL,null,Swlvia Smith347-933-6630sIrena Brown 347-991-1346s5 x week Mon-Fri
28434698,4Eleven,2013-09-19T19:57:23Z,112 West 28th Street, 3th Fl,sNY, 10018s917-922-3862s646-710-4170,null,null
You could try the following.
perl -nle '@F = split(/,(?!s| )/, $_); print $1 while ($F[3] =~ /((\d{3}-){2}\d{4})/g)' file.csv
Output
212-409-1641
248-469-5859
917-932-3962
646-710-4170
917-922-3862
646-710-4170
Upvotes: 1
Reputation: 4550
An answer using awk
:
awk -F'^' '{
start = 0;
str = substr($4, start);
while (match(str, /([0-9]{3})-[0-9]{3}-[0-9]{4}/)) {
print substr(str, RSTART, RLENGTH);
start = RSTART + RLENGTH;
str = substr(str, start);
}
}' datafile
This takes the 4th column, repeatedly matches the phone pattern, and prints it out on a line.
Upvotes: 0
Reputation: 703
Grep can use the perl or posix standard with -P or -E. See man grep for details. For something like this, I normally use cut to separate fields first, assuming that none of the fields will ever contain the column delimiter.
echo "a,b,c,123-555-1212,d,e,f" | cut -f 4 -d','
or from a file,
while read line; do
c4=$(echo $line | cut -f 4 -d',')
done < /tmp/file.csv
If any of the columns can contain commas then you're probably better off switching to a CSV library in ruby, python, etc.
UPDATE: using -d'^' to separate columns, you can pretty easily match the columns you're interested in, as above, the tricky part with sed is extracting the phone numbers,
f="80 W 54th St, Penthouse & 4th Fl, NY, 10018s212-409-1641"
echo $f | sed -r 's/(.*?)([0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$)/\2/'
212-409-1641
Not that you have to use the extended regex sed command line argument (-r) cannot seem to use regex literals like \d{3}. The documentation for sed is found in the info page, but it's usually easier to grep the net. This is a pretty good tutorial: http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/
Upvotes: 0