Redian新闻
>
my homework for UNIX shell高手, thanks!!!
avatar
my homework for UNIX shell高手, thanks!!!# Unix - 噫吁兮,危乎高哉
a*n
1
how to access a table in file, e.g.
A FILE called "hobbies", with contents:
zhang3, fishing
li4, playing basketball
wang5, watching TV
...
how to write a shell program "table", can handle a table like "hobbies"
$ table hobbies
Mr. zhang3 loves fishing;
Mr. li4 loves playing basketball;
Mr. wang5 loves watching TV;
...
avatar
p*f
2

if perl is allowed...
#!perl -p
s/^/Mr. / && s/,/ loving/ && s/$/;/ or die "data format error"

【在 a*******n 的大作中提到】
: how to access a table in file, e.g.
: A FILE called "hobbies", with contents:
: zhang3, fishing
: li4, playing basketball
: wang5, watching TV
: ...
: how to write a shell program "table", can handle a table like "hobbies"
: $ table hobbies
: Mr. zhang3 loves fishing;
: Mr. li4 loves playing basketball;

avatar
a*s
3


【在 a*******n 的大作中提到】
: how to access a table in file, e.g.
: A FILE called "hobbies", with contents:
: zhang3, fishing
: li4, playing basketball
: wang5, watching TV
: ...
: how to write a shell program "table", can handle a table like "hobbies"
: $ table hobbies
: Mr. zhang3 loves fishing;
: Mr. li4 loves playing basketball;

avatar
d*g
4
#!/bin/sh
cat $1 | while read line; do
NAME="`echo $line | cut -d, -f1`"
HOBBIE="`echo $line | cut -d, -f2`"
echo "Mr. $NAME loves $HOBBIE"
done
or
#!/bin/sh
cat $1 | while read line; do
echo "Mr. `echo $line | cut -d, -f1` loves `echo $line | cut -d, -f2`"
done

【在 a*******s 的大作中提到】

avatar
a*n
5
Thanks!
高手高手..

【在 d*****g 的大作中提到】
: #!/bin/sh
: cat $1 | while read line; do
: NAME="`echo $line | cut -d, -f1`"
: HOBBIE="`echo $line | cut -d, -f2`"
: echo "Mr. $NAME loves $HOBBIE"
: done
: or
: #!/bin/sh
: cat $1 | while read line; do
: echo "Mr. `echo $line | cut -d, -f1` loves `echo $line | cut -d, -f2`"

avatar
k*j
6
Another approach:
#!/bin/sh
sed -e 's/\(.*\), *\(.*\)/Mr. \1 loves \2;/' $1

-f2`"
"hobbies"

【在 a*******n 的大作中提到】
: Thanks!
: 高手高手..

avatar
f*r
7
awk -F, '{printf "Mr. %s loves %s;\n",$1,$2}' hobbies > table

【在 a*******n 的大作中提到】
: how to access a table in file, e.g.
: A FILE called "hobbies", with contents:
: zhang3, fishing
: li4, playing basketball
: wang5, watching TV
: ...
: how to write a shell program "table", can handle a table like "hobbies"
: $ table hobbies
: Mr. zhang3 loves fishing;
: Mr. li4 loves playing basketball;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。