用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脚本实例,展示了如何使用Shell语言处理文本文件,包括读取、解析并展示文本数据。
678

被折叠的 条评论
为什么被折叠?



