结果:

结果:

这些是文件:

项目2.电话:

  Dave,7348389800
  Barry,3131234567
  Carl,2483445576
  Marci,3134491390
  Tom,2484962204
  Alden,6165564458 

项目2.day:

  Barry,tuesday
  Carl,friday
  Tom,sunday
  Alden,wednesday
  Dave,thursday
  Marci,saturday

我的输出如下所示:

Day            Name         Phone
 ____________________________________
SUNDAY         Tom       2484962204
MONDAY         Nobody
TUESDAY        Barry     3131234567
WEDNESDAY      Alden     6165564458
THURSDAY       Dave      7348389800
FRIDAY         Carl      2483445576
SATURDAY       Marci     3134491390
SUNDAY         Tom       2484962204

我需要输出如下所示:

Day            Name         Phone
 ____________________________________
SUNDAY         Tom      (248)496-2204
MONDAY         Nobody
TUESDAY        Barry    (313)123-4567
WEDNESDAY      Alden    (616)556-4458
THURSDAY       Dave     (734)838-9800
FRIDAY         Carl     (248)344-5576
SATURDAY       Marci    (313)449-1390
SUNDAY         Tom      (248)496-2204

我无法获取正确格式的电话号码。

仅当我使用时才有效awk -F, -fproject2.kshproject2.phoneproject2.day

我需要能够运行 ksh project2.ksh 才能获取输出。

这是脚本:

项目2.ksh


      BEGIN     { split("SUNDAY;MONDAY;TUESDAY;WEDNESDAY;THURSDAY;FRIDAY;SATURDAY", week, ";") }


      FNR == NR { name2phone[$1] = gensub(/([0-9]{3})([0-9]{3})(.*)/, "(\\1) \\2-\\3", "g", $2 )
        next
      }

      { day2name[toupper($2)] = $1 }

           END       { fmtstr= "%-10s\t%s\t%s\n"

                         printf(fmtstr,"Day","Name","Phone")

                         print "____________________________________"

                     for(d=1;d< length(week); d++ )
                     {

                         cname = (week[d] in day2name ) ? day2name[ week[d] ] : "NOBODY"

                         printf( fmtstr, week[d], cname, name2phone[ cname])
                     }
      }

答案1

GNU awk 的一些想法:

$ awk '{match($0,/(...)(...)(.*)/,a);printf("(%s)%s-%s\n",a[1],a[2],a[3])}' <<<"333456789"
(333)456-789

$ awk '{match($0,/(...)(...)(.*)/,arr);printf( "(" arr[1] ")" arr[2] "-" arr[3])}' <<<"333456789"
(333)456-789

$ awk '{a=$0;printf("(%s)%s-%s\n",substr(a,1,3),substr(a,4,3), substr(a,7))}' <<<"333456789"
(333)456-789

要使其适应您的脚本,请尝试以下操作:

替换最后一个脚本行:

printf( fmtstr, week[d], cname, name2phone[ cname])

有了波纹管,应该可以正常工作:

ph=name2phone[ cname];
match(ph,/(...)(...)(.*)/,arr);
printf( fmtstr, week[d], cname, "(" arr[1] ")" arr[2] "-" arr[3])

答案2

{
  raw=$3; //or whichever column the phone# is in
  area=substr(raw,1,3);
  prefix=substr(raw,4,3);
  suffix=substr(raw,7);
  print $1, $2, "("area")"prefix"-"suffix 
}

为我工作

答案3

perl -F, -lane '

@ARGV and $Phone_of{$F[0]} = $F[1] =~ s/(...)(...)(.*)/($1)$2-$3/r;

@ARGV or $Person_of{uc $F[1]} = $F[0];

END {
   $fmt = join "\t", map { qq/%${_}s/ } qw/ -10 1 1 /;
   print sprintf $fmt, qw/ Day Name Phone /;
   print "_" x 37;
   for my $day ( map { uc($_ . day) } qw/ sun mon tues wednes thurs fri satur / ) {
      print sprintf $fmt, $day, $Person_of{$day} || "Nobody", $Phone_of{$Person_of{$day}};
   }
}

' project2.phone project2.day

%Phone_of我们首先用键作为名称填充哈希,并用值填充他们的电话号码。接下来,当它变为零时,即正在处理最后一个文件(.day)时,我们用大写日期的键和当天工作的人员的值@ARGV填充散列。%Person_of

END{...}块中,当两个文件都被处理掉后,我们首先设置格式字符串并使用它打印标题。最后,我们循环一周中的几天(大写)并显示这一天、当天工作的人员(如果当天没有人工作,则显示无人)以及该人员的电话号码。

结果:

Day             Name    Phone
_____________________________________
SUNDAY          Tom     (248)496-2204
MONDAY          Nobody
TUESDAY         Barry   (313)123-4567
WEDNESDAY       Alden   (616)556-4458
THURSDAY        Dave    (734)838-9800
FRIDAY          Carl    (248)344-5576
SATURDAY        Marci   (313)449-1390

注意事项

No whitespace(s) in the input files (*.phone, *.day)

相关内容