Reputation: 25
I need to extract from this line:
Susan Dalsass:(206) 654-6279:250:60:50 ----> (the line is in the susan.txt file)
only this strings:
Susan Dalsass (206) 654-6279
using awk and regex pattern. Maybe someone knows how to do it?
I tried to do something like this but it didn't work:
awk '/(Susan)[[:space:]](Dalsass).?([0-9]{3})[[:space:]]([0-9]{3}-[0-9]{4})/{print $1,$2,$3,$4}' susan.txt
Upvotes: 1
Views: 66
Reputation: 35556
Assumptions:
<name>:<phone>:<rest_of_line>
name
(first) fieldSetup:
$ cat susan.txt
ignore this line
Susan Dalsass:(206) 654-6279:250:60:50
ignore this line
For this particular case there's really no need to use a (complicated?) regex when we can use some basic string matching capabilities of awk
...
Exact match on first field:
$ awk -F':' '$1=="Susan Dalsass" { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279
Match on leading part of the input line:
$ awk -F':' '/^Susan Dalsass/ { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279
Using a bash
variable for an exact match on the name:
$ fullname="Susan Dalsass"
$ awk -v name="${fullname}" -F':' '$1==name { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279
Using a bash
variable to do a partial match on the name:
$ partname="Susan"
$ awk -v name="${partname}" -F':' '$1~name { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279
Using a bash
variable to do a partial, case-sensitive match on the name:
$ partname="saSS"
$ awk -v name="${partname}" -F':' 'tolower($1)~tolower(name) { print $1,$2 }' susan.txt
Susan Dalsass (206) 654-6279
Upvotes: 1