MHmmm
MHmmm

Reputation: 25

How to extract a specific line strings using awk

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

Answers (1)

markp-fuso
markp-fuso

Reputation: 35556

Assumptions:

  • the line(s) of interest are of the form <name>:<phone>:<rest_of_line>
  • matching will be based solely on the name (first) field
  • the full name (spelling and case) is known in advance otherwise we need to look at modifying the match logic to work on a) a substring and/or b) case sensitivity

Setup:

$ 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

Related Questions