欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

测试工作常用Linux shell命令-去除空白行及两种空白行去除时的注意事项(基于sed)

程序员文章站 2022-06-04 19:29:00
...

日常工作中,有些文档里的内容可能并不都是我们想要的,比如说一些注释什么的。
如下进入/etc/passwd,发现前10行是有注释的,也保不准其他还有什么地方有注释,那么现在需求就是:去除内容中的注释行,并且产生的空白行也不要。

     1  ##
     2  # User Database
     3  #
     4  # Note that this file is consulted directly only when the system is running
     5  # in single-user mode.  At other times this information is provided by
     6  # Open Directory.
     7  #
     8  # See the opendirectoryd(8) man page for additional information about
     9  # Open Directory.
    10  ##
    11  nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
    12  root:*:0:0:System Administrator:/var/root:/bin/sh
    13  daemon:*:1:1:System Services:/var/root:/usr/bin/false
    14  _uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
    15  _taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
    ......#后面的内容省略

例一:

  • 步骤一:先去除以#开头的注释行
    命令:cat testpasswd | sed 's/^#.*$//g' | less
    结果:
[[email protected] ~]$ cat Allen_qin/testpasswd | sed 's/^#.*$//g' | less











nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
_lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
_postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false
_scsd:*:31:31:Service Configuration Service:/var/empty:/usr/bin/false
_ces:*:32:32:Certificate Enrollment Service:/var/empty:/usr/bin/false
  • 步骤二: 删除空白行
    命令:cat /etc/passwd | sed 's/^#.*$//g' | sed '/^$/d'| less
    结果:
[[email protected] ~]$ cat Allen_qin/testpasswd | sed 's/^#.*$//g' | sed '/^$/d'| less
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
_lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
_postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false
_scsd:*:31:31:Service Configuration Service:/var/empty:/usr/bin/false
_ces:*:32:32:Certificate Enrollment Service:/var/empty:/usr/bin/false
_appstore:*:33:33:Mac App Store Service:/var/empty:/usr/bin/false
......#后面内容省略

这样就完成了空白行的删除,下面再对上面的例子进行“相同”操作(加了双引号的相同~)来看看现象:

例二:

  • 步骤一:还是先去除以#开头的注释行
    命令:cat testpasswd | sed 's/^#.*$/ /g' | less
    结果:
[[email protected] ~]$ cat Allen_qin/testpasswd | sed 's/^#.*$/ /g' | less










nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
_lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
_postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/b
  • 步骤二:还是使用与例一相同的命令对空白行进行删除
    命令:cat /etc/passwd | sed 's/^#.*$/ /g' | sed '/^$/d'| less
    结果:
[[email protected] ~]$ cat Allen_qin/testpasswd | sed 's/^#.*$/ /g' | sed '/^$/d'| less









nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false

这个时候奇怪的现象发生了,空白行并没有被成功删除!两次操作几乎是一模一样的,这里我们把两个例子中步骤一的命令放在一起对比一下:

cat /etc/passwd | sed 's/^#.*$//g'  #例一
cat /etc/passwd | sed 's/^#.*$/ /g' #例二

应该很明显的看出问题所在了,在例一中我将注释行内容替换为了,而例二中我将注释行替换为了空格,而步骤二中的正则匹配^$只能匹配到,所以例二中的空白行就没有被删除,其实只是肉眼看起来一样,实质上例二中每行都是由空格组成;

  • 如果想要将例二这种空白页删除的话,可以借助于正则表达式中的\s(匹配任意的空白符),命令如下:
    cat Allen_qin/testpasswd | sed 's/^#.*$/ /g' | sed '/^\s*$/d' | less
    结果:
[[email protected] ~]$ cat Allen_qin/testpasswd | sed 's/^#.*$/ /g' | sed '/^\s*$/d' | less
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
_lp:*:26:26:Printing Services:/var/spool/cups:/usr/bin/false
_postfix:*:27:27:Postfix Mail Server:/var/spool/postfix:/usr/bin/false
_scsd:*:31:31:Service Configuration Service:/var/empty:/usr/bin/false
_ces:*:32:32:Certificate Enrollment Service:/var/empty:/usr/bin/false
_appstore:*:33:33:Mac App Store Service:/var/empty:/usr/bin/false
_mcxalr:*:54:54:MCX AppLaunch:/var/empty:/usr/bin/false
_appleevents:*:55:55:AppleEvents Daemon:/var/empty:/usr/bin/false
......#后面内容省略

这样也就完成了空白行的删除工作了~