Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK script for splitting a line 1

Status
Not open for further replies.

hmehta

IS-IT--Management
Jun 5, 2002
27
US
I have a file with phone nos 9728993456 as the first filed. I need to split inoo 972-899-3456 . How can I do it in awk.
 
awk ' {
if (length($1) == 10 && $1 ~ /[0-9]+/) {
cnt++
numbers["area", NR] = substr($1,1,3) "-"
numbers["prefixes", NR] = substr($1,4,6) "-"
numbers["spec", NR] = substr($1,7,10)
}

for (xx in numbers) {
for (m=1 ; m <= cnt ; m++) {
if (xx ~ m) {
printf &quot;%s&quot;, numbers[xx]
}; }; }
}' file

Might work, not tested.
 
These work well...

awk '{
print substr($0,1,3) &quot;-&quot; substr($0,4,3) &quot;-&quot; substr($0,7,4)
}' file

sed 's/\(.\{3\}\)\(.\{3\}\)/\1-\2-/' file
sed 's/\(...\)\(...\)/\1-\2-/' file

The last one runs the fastest.

Cheers,
ND [smile]



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top