UNIX:如何使用循环结构从2个文件输出数据?

时间:2015-11-20 20:54:13

标签: linux shell unix

大家下午好,我有一个我正在做的项目,但我遇到了一些麻烦。我应该创建一个脚本,使用循环结构从2个数据文件输出数据。

这是两个数据文件:

DATA1:

Dave,7348389800
Bob,3131234567
Carol,2483445576
Mary,3134491390
Ted,2484962204
Alice,6165564458

DATA2:

Bob,tuesday
Carol,friday
Ted,sunday
Alice,wednesday
Dave,thursday
Mary,saturday

这就是我在显示时的外观:

Day       Name     Phone
__________________________________________
MONDAY     Nobody
TUESDAY    Bob    (313) 123-4567
WEDNESDAY  Alice  (616) 556-4458
THURSDAY   Dave   (734) 838-9800
FRIDAY     Carol  (248) 344-5576
SATURDAY   Mary   (313) 449-1390
SUNDAY     Ted    (248) 496-2204

这是我目前的代码:

#!/bin/ksh
for day in monday tuesday wednesday thursday friday saturday sunday
    do
    day=`grep -i day data2 |cut -d "," -f 2 `
    name=`cut -d "," -f 1 data1 `
    phone=`cut -d "," -f 2 data1`
done
echo  $day $name $phone >>output

这是我得到的输出:

Day Name Phone
==============
tuesday friday sunday wednesday thursday saturday Dave Bob Carol Mary Ted Alice 7348389800 3131234567 2483445576 3134491390 2484962204 6165564458

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

这可行,使用printf来获得整齐格式的列式输出:

format="%-9s  %-9s  %s\n"

printf "$format" Day Name Phone
printf "$format" Day Name Phone | sed 's/./-/g'

for day in monday tuesday wednesday thursday friday saturday sunday
do
    who=$(grep -i "$day" data2 | cut -d "," -f 1)
    if [ -z "$who" ]
    then
        who="NOBODY"
        phone=""
    else
        phone=$(grep -i "$who" data1 | sed 's/^[^,]*,\(...\)\(...\)\(....\)/(\1) \2-\3/')
    fi
    printf "$format" $(echo "$day" | tr 'a-z' 'A-Z') "$who" "$phone"
done

您可以调查typeset -u uday="$day",然后将"$uday"传递给printf;这似乎不是Bash 3.x中的一个选项,但它适用于Korn shell和Bash 4.x(对于足够大的x值)。 [...考虑到这一点,grep | cutgrep | sed序列可以(也可能应该)被纯sed取代,而printf仍然更整洁。您还可以使用sed 'p;s/./-/g'避免重复Day Name Phone --------------------------- MONDAY NOBODY TUESDAY Bob (313) 123-4567 WEDNESDAY Alice (616) 556-4458 THURSDAY Dave (734) 838-9800 FRIDAY Carol (248) 344-5576 SATURDAY Mary (313) 449-1390 SUNDAY Ted (248) 496-2204 来处理标题。修复这些问题留给读者练习......]

输出:

let predicate = CNContact.predicateForContactsMatchingName("john")

let toFetch = [CNContactGivenNameKey, CNContactFamilyNameKey]

do {
     let contacts = try store.unifiedContactsMatchingPredicate(
          predicate, keysToFetch: toFetch)

      for contact in contacts{
          print(contact.givenName)
          print(contact.familyName)
          print(contact.identifier)
      }

} 
catch let err {
        print(err)
}

答案 1 :(得分:0)

要求救援!

$ awk -F, -vOFS="\t" '
     BEGIN{split("1monday 2tuesday 3wednesday 4thursday 5friday 6saturday 7sunday",days," ")} 
   NR==FNR{a[$1]=$2;next} {b[$2]=$1} 
       END{for(i in days) {
            d=substr(days[i],2,length(days[i])-1); 
            dp=toupper(days[i]); 
            if(b[d]) 
               print dp,b[d],a[b[d]]; 
            else 
               print dp,"Nobody"}}' data1 data2 
 | sort | cut -c2- | column -t 
 | sed -r 's/([0-9]{3})([0-9]{3})([0-9]{4}$)/(\1) \2-\3/'


MONDAY     Nobody
TUESDAY    Bob     (313) 123-4567
WEDNESDAY  Alice   (616) 556-4458
THURSDAY   Dave    (734) 838-9800
FRIDAY     Carol   (248) 344-5576
SATURDAY   Mary    (313) 449-1390
SUNDAY     Ted     (248) 496-2204