I am attempting to format two pieces of data from my awk script. Here is a piece of my raw data
Mike:James:314849866:[email protected]:5059358554:NM:8830:Johnson:Rd:Albuquerque:87122
There are a total of nine lines like this. I have formatted it in this manner
Mike James, 314849866
8830 Johnson Rd
Albuquerque, NM 87122
[email protected]
5059358554
using this code:
cat rawadd | awk -F: ' NR == 1,NR == 9 {print $1 " " $2 ", " $3 "\n" $7 " " $8 " " $9 "\n" $10 ", " $6 " " $11 "\n" $4 "\n" $5
I would like to format the phone number, the fifth line like (505)935-8554. So I created a new variable $tel, and replaced it with the $5 variable that I extracted from the rawadd file.
Here is what that new code looks like:
tel=`"(${5:0:3}) ${5:3:3}-${5:6:4}"`
cat rawadd | awk -F: ' NR == 1,NR == 9 {print $1 " " $2 ", " $3 "\n" $7 " " $8 " " $9 "\n" $10 ", " $6 " " $11 "\n" $4 "\n" $tel "\n"}';
But my output is coming out like this
Mike James, 314849866
8830 Johnson Rd
Albuquerque, NM 87122
[email protected]
Mike:James:314849866:[email protected]:5059358554:NM:8830:Johnson:Rd:Albuquerque:87122
On the fifth line it just prints the actual line input, and not the formatted telephone number. I was hoping that I might add the formatting directly into the awk command, but cannot figure out a way. I would also like to format the Id number on the first line to be 314-84-9866. Any help would be great.
Thank you
$telvariable is not visible inawk, within single quotes bash variables are not substituted. Even it was visible the way you coded doesn't work inawk. Instead you have to use"(" substr($5,1,3) ")" .. etcin theawkscript.cat rawaddis adding.