shell脚本 逐行读取文本并且 进行字符串的截取

这篇博客介绍了如何使用Shell脚本来逐行读取文本文件,并进行字符串截取操作。示例中,脚本通过`read`命令读取文件内容,然后利用参数扩展来去除字符串右侧空格后的字符,判断并输出相同类型的行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; line-height: 21.6px;">例如有个文本文件</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; line-height: 21.6px;">1234  ji</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; line-height: 21.6px;">adfa jiji</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; line-height: 21.6px;">adfasd qqwe</p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; line-height: 21.6px;">    // 注意一定要有个回车,这样脚本才会读到最后一行 </p><div>
</div>








while read typeline
do
#get line txt
typeInf=$typeline
#get type
#去除右边第一个空格开始的后边所有字符
typetmp=${typeInf%% *}
#judge type same
if [ "$typetmp" = "$type" ]
then
echo sametype:$typetmp
#get count
pcount=${typeInf:0-1}
fi
done < $file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值