👋 欢迎来到吴老二技术博客,专注运维 / 数据库 / 云原生
首页/运维服务/正文

shell读取文件中多列的行

吴
吴老二
2021-01-12运维服务2889 次浏览0 条评论
在文件中有多列,通常读取的时候会把一行分为多行进行读取,这样就不能达到使用的目的,看下面的例子:
[root@wulaoer ~]# cat wulaoer.org 
python golang linux centos
nginx devops java springboot
jenkins ldap django mysql
redis mongodb es mq
zabbix prometheus pinpoint grafana
这是我文件中的内容,分了四段,如果我使用cat读取显示没有问题,使用for循环时,会循环的字符串每段都会进行换行
[root@wulaoer ~]# for i in `cat wulaoer.org`;do echo $i;done
python
golang
linux
centos
nginx
devops
java
springboot
jenkins
ldap
django
mysql
redis
mongodb
es
mq
zabbix
prometheus
pinpoint
grafana
这个结果不是想要的,所以无法使用,那就换个方法吧。看下面的循环 方法一:
[root@wulaoer ~]# while read line;do echo "######",$line;done < wulaoer.org 
######,python golang linux centos
######,nginx devops java springboot
######,jenkins ldap django mysql
######,redis mongodb es mq
######,zabbix prometheus pinpoint grafana
######,
方法二:
[root@wulaoer ~]# cat wulaoer.org | while read myline;do echo "###",$myline;done
###,python golang linux centos
###,nginx devops java springboot
###,jenkins ldap django mysql
###,redis mongodb es mq
###,zabbix prometheus pinpoint grafana
这样就可以满足我的需求了,可以根据这个在执行shell文件多个参数时做批量处理。
文章底部广告位在后台「广告设置」填写广告代码后显示

评论 0

评论需 登录 后发表(注册并验证邮箱)。
还没有评论,来抢沙发~