当前位置:网站首页>Shell 按行读取文件
Shell 按行读取文件
2022-06-11 02:36:00 【墨痕诉清风】
背景
做批量文件替换、删除时,读取文件内容已写好的文件名,存入数组中,遍历进行for循环批量替换。
写法一
#!/bin/bash
#描述:
# while循环读行操作
while read line
do
echo $line
done < filename写法二(这个循环在动态添加数组后数组中数据丢失)
#!/bin/bash
#描述:
# cat后读行操作
cat filename | while read line
do
echo $line
done写法三
#!/bin/bash
#描述:
# for循环读行操作,与while读行有区别
for line in `cat filename`
do
echo $line
done注
for循环读行操作,与while读行的区别
while是完全按行读取,不管行内有多少段文字
for是按行读取,如果行内文字有空格,则分开读取,即一次读取一个字符串
边栏推荐
猜你喜欢

蓝桥杯_小蓝吃糖果_鸽巢原理 / 抽屉原理

Arduino使用NRF24L01模块进行无线通信

完成千万元A轮融资,小象生活能否成为折扣界的“永辉”?

When the interviewer opens his mouth, he comes to compose. Is this the case now?
![[189. rotation array]](/img/cc/0da616ad9adc9c9d352e54f58dbe41.png)
[189. rotation array]
![【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析](/img/77/30215c363ae8a1324db1cbcaa5324b.png)
【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析
![[resolved] how to fix another update in progress WordPress upgrade error](/img/b1/7b9945c1a623c20288f0d232cbb0c4.jpg)
[resolved] how to fix another update in progress WordPress upgrade error

Flat data to tree and tree data flattening

从绿联冲刺IPO,看手机配件市场沉浮录
![[AI weekly] AI and freeze electron microscopy reveal the structure of](/img/2e/e986a5bc44526f686c407378a9492f.png)
[AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso
随机推荐
Write my Ini configuration file error
Construction of Flink development environment and wordcount
[implementation of bubble sorting]
Necessity for banks to choose electronic bidding procurement
AOSP ~ logcat chatty line expired
Wechat template message errCode ": 40165," errmsg ":" invalid web pagepath
CPT 102_LEC 16
Error excluded identifier before '(' token, grpc enumeration class compilation error
AOSP ~ 默认开启开发者模式
CPT 102_ LEC 18
CPT 102_LEC 20
富络经典源码富络经典系统开发原理分享
怎样确保消息的可靠性投递?
Recent learning and update plan
CPT 102_ LEC 17
Application of the remote acquisition IOT gateway of the Bashir trough flowmeter in open channel flow monitoring
新来的同事问我 where 1=1 是什么意思???
The new colleague asked me what "where 1=1" means???
基于互联网架构演进, 构建秒杀系统
20220610 星期五