用shell简单处理文本的例子

本文通过一个简单的Shell脚本实例,展示了如何使用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的输入。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值