用shell简单处理文本的例子

用shell简单处理文本的例子

text_process.sh

#!/bin/sh

IFS=:

echo "example of text processing using shell"
echo "--------------------------------------"

while read name age university
do
echo "$name is from $university, $age"
done < ./raw_txt.txt

raw_txt.txt

zhang san:20:Hunan Univerisity
li si:21:Tsinghua Univerisity
wang wu:22:Wuhan Univerisity
执行:

xxx:/work/shell/text_process$ sh text_process.sh > result.txt
xxx:/work/shell/text_process$ cat result.txt
example of text processing using shell
--------------------------------------
zhang san is from Hunan Univerisity, 20
li si is from Tsinghua Univerisity, 21
wang wu is from Wuhan Univerisity, 22

注意:

1. 上面IFS=:指定了name、age、university shell变量之间的分割符。

2. 当读到最后一行时while条件不符合,结束循环。

3. while done后面的 < ./raw_txt.txt是重定向while read的输入。




你可能感兴趣的:(Shell)