用shell简单处理文本的例子

来源:互联网 发布:淘宝介入后买家输后果 编辑:程序博客网 时间:2024/05/25 12:21

用shell简单处理文本的例子

text_process.sh

#!/bin/shIFS=:echo "example of text processing using shell"echo "--------------------------------------"while read name age universitydoecho "$name is from $university, $age"done < ./raw_txt.txt

raw_txt.txt

zhang san:20:Hunan Univerisityli si:21:Tsinghua Univerisitywang 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的输入。




0 0
原创粉丝点击