a*******n 发帖数: 2 | 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;
... | p******f 发帖数: 162 | 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;
| a*******s 发帖数: 324 | 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;
| d*****g 发帖数: 36 | 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 的大作中提到】
| a*******n 发帖数: 2 | 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`"
| k*j 发帖数: 2 | 6 Another approach:
#!/bin/sh
sed -e 's/\(.*\), *\(.*\)/Mr. \1 loves \2;/' $1
-f2`"
"hobbies"
【在 a*******n 的大作中提到】 : Thanks! : 高手高手..
| f*****r 发帖数: 229 | 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;
|
|