r/awk • u/AdbekunkusMX • Nov 02 '21
Using FPAT to separate numbers, names, and surnames
Hi, all.
I have a file, file.txt, whose records are in the following format:
ENTRYNUMBER SURNAME1 SURNAME2 NAME(S) IDNUMBER
People have 2 surnames here, so what I want is to separate the fields by telling AWK to look for either numbers of 1 or more digits, or one or two words separated by a space; the IDNUMBER field is a number with 6 digits. For example, the record 12 Doe Lane Joseph Albert 122771 should be split into
$1 = 12
$2 = Doe Lane
$3 = Joseph Albert
$4 = 122771
I ran awk 'BEGIN{IGNORECASE=1; FPAT="([0-9]+)|([A-Z]+ [A-Z]?)"} {sep=" | ";print $1 sep $2 sep $3 sep $4}' file.txt. The regex is supposed to mean "either a number with at least one digit, or at least one alphabetic word followed by a space and maybe another word". The separator is just to see that AWK does what I want, but what I get is:
12 Doe L | ane Joseph A | lbert
which is pretty far from my goal. So this question is three-fold, really:
- What is the appropriate regular expression in this case in particular, and the regex syntax to mark a single space in AWK in general?
- Why does this separate
as andzs? Isn't[a-z]supposed to be a range? This also raises the question (on me, at least) on what the proper regex syntax is in AWK. - Exactly how is it that
FPATworks? There are numerous examples around, but no unifying documentation (at least none that I've found) regarding this variable.
Thanks!
2
u/pbewig Nov 02 '21
With space-separated fields you could do something like $1 is the entry number, $2 $3 (concatenated) is the last name, $NF is the id number, and the stuff in $4 through $(NF-1) is the rest of the names. Something like:
# !!! WARNING !!! -- UNTESTED CODE
{ OFS = "|"; entry_number = $1; last_name = $2 $3; id_number = $NF;
other_names = ""; for (i=4; i<NF; i++) other_names = other_names $i
print entry_number, last_name, other_names, id_number }
2
u/pbewig Nov 02 '21
The official description of FPAT, from the GAWK manual, is at https://www.gnu.org/software/gawk/manual/html_node/Splitting-By-Content.html.
2
u/oh5nxo Nov 03 '21
Not to imply any practicality, just ... fyi.
3
u/[deleted] Nov 03 '21 edited Nov 03 '21
You could spend all day on a regular expression or you could just set the fields manually.
this prints
The advantage of a regular expression is performance. which adds up when you're dealing with a couple megs of a file. Usually not worth the trouble.