简体   繁体   中英

Sed or awk extract phone number in whole file

I'am working on parsing SMS numbers from email messages and I got one problem. I'am using awk and sed in bash script. The format of phone number is SMS: +XXX XXX XXX XXX . When there is this string included in email body i need to send SMS with subject. All working well so far but I got an issue when there is this string multiple on one line or after some web link. Currently I'am using this to parse phone numbers into array and then create files with number in name.

phone=( $( awk /SMS/ $FILE | awk '{ gsub (" ", "", $0); print}' ) )

Works fine when there is SMS string on separate lines. I need same to parse string when there is more strings on one line: SMS: +123 456 789 123 SMS: +456 789 123 456 or http://somelink/to/some/web/page.html SMS: +123 456 789 123 Basically it would be best to parse whole file and find anything after SMS: which is in format +XXX XXX XXX XXX . Also it is not sure that after last digit will be line break - so it might be between 2 separate strings. Thanks for help.

grep -o will make your life easier:

grep -oE 'SMS: \+([[:digit:]]{3} ?){4}' "$file"

That will spit out each match on a separate line, no matter how many occur on the same line in the input.

Also, get out of the habit of using ALL_CAPS_VARNAMES. One day you'll accidentally use PATH=... and then wonder why your script is broken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM