说明

本文主要说明 如何去除 文档中的 注释行、空白行以及过滤空格。

去除空格说明

  • 去除空格会涉及到 正则关系,我先在这对一些符号做下说明,下面有看不懂的地方回来看这的解析!!!
  • 如:sed 's/^[ ]*//g'表示使用空字符匹配行首为空格的字符串;
  • 依次拆解释义:
    • 字符's'表示替换;
    • 字符'/' 表示作为分隔符;
    • 字符 '^'表示开头;
    • 字符'$' 表示结尾;
    • 字符'*'表示所有;
    • 字符'[ ]'表示匹配指定字符范围内的任意单个字符,[ ]中含一个空格;
    • 字符'g’表示全部匹配;
  • 去除空格的使用场景可以分为:去除行首空格、去除行尾空格以及去除所有空格,我下面单独说明

去除行首空格

  • 命令:sed 's/^[ ]*//g'
  • 用脚本展示使用方法和效果吧:
[root@centos76_1 ccx]# sh space.sh
     def    ——未去除空格
def    ——已去除行首空格
[root@centos76_1 ccx]# 
[root@centos76_1 ccx]# 
[root@centos76_1 ccx]# cat space.sh 
#!/bin/bash

str='     def    '
echo "$str——未去除空格"
echo "$str——已去除行首空格" | sed 's/^[ ]*//g'
[root@centos76_1 ccx]# 

去除行尾空格

  • 命令:sed 's/[ ]*$//g'
  • 用脚本展示使用方法和效果吧:
    注:这个比较特殊,用字符串数量的形式来证明已经去除了(当时搞了好久,以为出问题了。。。)
[root@centos76_1 ccx]# sh space.sh 
未去空格前字符串数量:12
去除空格后字符串数量:7
    ccx     后面加东西后依然存在
[root@centos76_1 ccx]# 
[root@centos76_1 ccx]# cat space.sh
#!/bin/bash

#str='     defc         '
#echo "$str" | sed 's/[ ]*$//g'

var='    ccx     '
var1=$(echo "${var}")
echo "未去空格前字符串数量:${#var1}"
var2=$(echo "${var}" |sed 's/[ ]*$//g')
echo "去除空格后字符串数量:${#var2}"
echo "$var""后面加东西后依然存在" | sed 's/[ ]*$//g'
[root@centos76_1 ccx]# 

去除所有空格

  • 命令:sed 's/[[:space:]]//g'
  • 用脚本展示使用方法和效果吧:
[root@centos76_1 ccx]# 
[root@centos76_1 ccx]# sh space.sh
     defc         ——start
defc——end
[root@centos76_1 ccx]# 
[root@centos76_1 ccx]# cat space.sh
#!/bin/bash

str='     defc         '
echo "$str"——start

echo "$str"——end | sed 's/[[:space:]]//g'

[root@centos76_1 ccx]# 

去除空白行

方法1(最简单)

  • 命令:grep -v "^$"
  • 前面任何查看的方式都行,这命令放管道符后面即可,效果如下
[root@centos76_1 ccx]# cat test.txt | grep -v "^$"
#dfda  sf
     dfadf
oodfadsf
fdf df ddf
daf da
    fff
ddd     
ddd ddf
# ddd
#aaa  cc
[root@centos76_1 ccx]# cat test.txt 
#dfda  sf
     dfadf
oodfadsf



fdf df ddf
daf da
    fff
ddd     


ddd ddf
# ddd
#aaa  cc

[root@centos76_1 ccx]# 

方法2(简单)

  • 命令:tr -s '\n'
  • 前面任何查看的方式都行,这命令放管道符后面即可,效果如下
[root@centos76_1 ccx]# cat test.txt | tr -s '\n'    
#dfda  sf
     dfadf
oodfadsf
fdf df ddf
daf da
    fff
ddd     
ddd ddf
# ddd
#aaa  cc
[root@centos76_1 ccx]# cat test.txt 
#dfda  sf
     dfadf
oodfadsf



fdf df ddf
daf da
    fff
ddd     


ddd ddf
# ddd
#aaa  cc

[root@centos76_1 ccx]# 

方法3

  • 命令:sed '/^$/d'
  • 前面任何查看的方式都行,这命令放管道符后面即可,效果如下
[root@centos76_1 ccx]# cat test.txt | sed '/^$/d' 
#dfda  sf
     dfadf
oodfadsf
fdf df ddf
daf da
    fff
ddd     
ddd ddf
# ddd
#aaa  cc
[root@centos76_1 ccx]# cat test.txt 
#dfda  sf
     dfadf
oodfadsf



fdf df ddf
daf da
    fff
ddd     


ddd ddf
# ddd
#aaa  cc

[root@centos76_1 ccx]# 

方法4

  • 命令:awk '{if($0!="")print}'
  • 前面任何查看的方式都行,这命令放管道符后面即可,效果如下
[root@centos76_1 ccx]# cat test.txt | awk '{if($0!="")print}' 
#dfda  sf
     dfadf
oodfadsf
fdf df ddf
daf da
    fff
ddd     
ddd ddf
# ddd
#aaa  cc
[root@centos76_1 ccx]# cat test.txt 
#dfda  sf
     dfadf
oodfadsf



fdf df ddf
daf da
    fff
ddd     


ddd ddf
# ddd
#aaa  cc

[root@centos76_1 ccx]# 

去除注释行

方式一【简单】

  • 命令:grep -v "^#" file
  • 前面任何查看的方式都行,这命令放管道符后面即可,效果如下
    下面命令我只是换了一种方法而已,与cat test.txt | grep -v "^#"同效果
[root@centos76_1 ccx]# grep -v "^#" test.txt 
     dfadf
oodfadsf#



fdf df# ddf
daf da
    fff
ddd     


ddd ddf

[root@centos76_1 ccx]# cat test.txt 
#dfda  sf
     dfadf
oodfadsf#



fdf df# ddf
daf da
    fff
ddd     


ddd ddf
# ddd
#aaa  cc

[root@centos76_1 ccx]# 

方式二【复杂】

  • 命令:grep -o '^[^#].*' file
  • 这种效果和上面一样的,只是这种是选中,上面种是过滤罢了,选中代码高亮,看得懂吧。
    在这里插入图片描述
    代码展示这2者效果
[root@etcd1 ~]# grep -o '^[^#].*' /etc/etcd/etcd.conf 
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://localhost:2380,http://192.168.59.156:2380"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://192.168.59.156:2379"
ETCD_NAME="default"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379,http://192.168.59.156:2379"
[root@etcd1 ~]#
[root@etcd1 ~]# grep -v "^#" /etc/etcd/etcd.conf 
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://localhost:2380,http://192.168.59.156:2380"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://192.168.59.156:2379"
ETCD_NAME="default"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379,http://192.168.59.156:2379"
[root@etcd1 ~]#

同时去除注释行和空白行

  • 其实就是把前面提到的2个命令放一条执行而已;
  • 命令:grep -v "^#" | grep -v "^$"
  • 前面任何查看的方式都行,这命令放管道符后面即可,效果如下
[root@centos76_1 ccx]# cat test.txt | grep -v "^#" | grep -v "^$"
     dfadf
oodfadsf#
fdf df# ddf
daf da
    fff
ddd     
ddd ddf
[root@centos76_1 ccx]# 
[root@centos76_1 ccx]# cat test.txt 
#dfda  sf
     dfadf
oodfadsf#



fdf df# ddf
daf da
    fff
ddd     


ddd ddf
# ddd
#aaa  cc

[root@centos76_1 ccx]# 
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐