結果:

結果:

這些是文件:

項目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)

相關內容