UNIX Z命令 - ZCAT,ZLUS,ZGREP,ZEGREP和ZDIFF示例

时间:2020-03-05 15:30:14  来源:igfitidea点击:

在本文中,让我们审查如何使用强大的Linux Z命令对压缩文件执行正常文件操作。
这些z命令的组件暂时在/tmp目录中解压缩文件以执行指定的操作。
一些z命令未压缩它以执行指定的操作。
但是,在任何情况下,Z命令都会给出安心,因为我们不想担心解压缩压缩文件以执行操作的开销。

我们可以在压缩文件上执行以下正常文件操作。

1)使用zcat查看压缩文件。
2)使用宽带/zmore分页压缩文件。
3)使用zgrep/zegrep搜索压缩文件内。
4)使用ZDIFF/ZCMP的文件比较

示例1:查看压缩文件并用zcat未压缩

使用Gzip压缩文件创建一个压缩文件,其中包含* .gz扩展名和所有.zip扩展文件,我们可以使用以下方式使用zcat查看压缩文件。
它与未压缩的文件操作'CAT Filename'同样。
zcat未压缩该文件并在stdout中显示它。

[shaha@oc8535558703 Desktop]$zcat sample_file.zip
CDKNMS4 524 694 651 735 812 671 553
CDKNMS5 484 389 1899 516 1307 241 1416
CDKNMS7 340 86 54 1597 24 1545 241
CDMBMS1 361 936 517 274 300 916 531
[shaha@oc8535558703 Desktop]$
[shaha@oc8535558703 Desktop]$zcat Compliance.gz
CDKNMS5 484 389 1899 516 1307 241 1416
CDKNMS7 340 86 54 1597 24 1545 241
CDMBMS1 361 936 517 274 300 916 531

示例2:查看没有.gz后缀的gzipped文件

我们可以解压缩没有GZ后缀的缩放文件。
如果我们尝试解压缩未使用"gunzip"或者"gzip -d"命令的zzipped文件,则会面临以下错误。

[shaha@oc8535558703 Desktop]$gunzip sample_file
gzip: sample_file: unknown suffix -- ignored
[shaha@oc8535558703 Desktop]$

但此zcat将解压缩文件并显示如下所示的内容。

[shaha@oc8535558703 Desktop]$cat > sample_file
This is a test file used for gunzip and zcat testing for this article
this is power of z command
[shaha@oc8535558703 Desktop]$gzip sample_file
[shaha@oc8535558703 Desktop]$mv sample_file.gz sample_file
[shaha@oc8535558703 Desktop]$gzip -d sample_file
gzip: sample_file: unknown suffix -- ignored
[shaha@oc8535558703 Desktop]$
[shaha@oc8535558703 Desktop]$zcat sample_file
This is a test file used for gunzip and zcat testing for this article
this is power of z command
[shaha@oc8535558703 Desktop]$

示例3:显示文件内容而不担心是否被压缩或者不

当我们不确定文件是否被压缩而且,我们仍然可以查看文件而不担心它的压缩状态,如下所示。

在此示例中,如果输入文件被压缩ZCAT将通过解压缩来显示内容。
如果输入文件未被压缩,则将显示内容。

[shaha@oc8535558703 Desktop]$zcat -f sample_file
CDKNMS4 524 694 651 735 812 671 553
CDKNMS5 484 389 1899 516 1307 241 1416
CDKNMS7 340 86 54 1597 24 1545 241

示例4:使用zless/zmore分页压缩文件

我们可以使用zless命令或者zmore命令分页压缩文件,如下所示。

[shaha@oc8535558703 Desktop]$zmore passwd_file.gz
------> passwd_file.gz <-----
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[shaha@oc8535558703 Desktop]$zless passwd_file.gz
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
systemfj:x:501:489::/opt/ibm/ibm-config-firejail/systemfj:/bin/bash
passwd_file.gz (END)

示例5:使用zgrep/zegrep搜索压缩文件内部

我们可以使用ZGREP/Zegrep在压缩文件中搜索,如下所示。
这与未压缩的文件操作'grep -i filename'同样。
zGrep命令的所有选项都将传递给Grep,并且该文件将被馈送到Grep命令。
如果需要,它可能会解压缩并馈送到Grep命令的文件。

1)使用压缩文件中的-n选项在文件中搜索文件中的模式以及相关的行号。

[shaha@oc8535558703 Desktop]$zgrep -n mysql passwd_file.gz
39:mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
[shaha@oc8535558703 Desktop]$

2)显示使用^ Symbo中的指定模式以压缩文件中的字样开头的所有行。

[shaha@oc8535558703 Desktop]$zgrep ^root passwd_file.gz
root:x:0:0:root:/root:/bin/bash
[shaha@oc8535558703 Desktop]$

3)在压缩文件中使用$符号显示以指定模式结尾的所有行。

[shaha@oc8535558703 Desktop]$zgrep bash$passwd_file.gz
root:x:0:0:root:/root:/bin/bash
shaha:x:500:501:Shaha Alam:/home/shaha:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
systemfj:x:501:489::/opt/ibm/ibm-config-firejail/systemfj:/bin/bash
[shaha@oc8535558703 Desktop]$

4)在压缩文件中使用zgrep搜索文件的所有空或者空行。

[shaha@oc8535558703 Desktop]$zgrep ^$passwd_file.gz
[shaha@oc8535558703 Desktop]$

5)在压缩文件中匹配模式之前和之后显示n行数。

a)在PATTEN匹配之前在压缩文件中使用-b选项显示四行。

root@Linux-shah:~# zgrep -B 4 "games" /etc/passwd

b)在压缩文件中使用-a选项显示模式匹配后显示四行。

root@Linux-shah:~# zgrep -A 4 "games" /etc/passwd

c)在压缩文件中使用-c选项显示图案匹配的四行。

root@Linux-shah:~# zgrep -C 4 "games" /etc/passwd

示例6:使用Zegrep压缩文件中的多个搜索

Zegrep命令具有多个正则表达式模式,以减少输出更多,eGrep命令在多个正则表达式上执行不敏感的搜索。

a)使用zegrep搜索压缩文件中的多个模式

[shaha@oc8535558703 Desktop]$zegrep 'shah|root|apache' passwd_file.gz
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
shaha:x:500:501:Shaha Alam:/home/shaha:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
[shaha@oc8535558703 Desktop]$

b)使用压缩文件中的-n选项在文件中搜索文件中的多个模式以及相关的行号

[shaha@oc8535558703 Desktop]$zegrep -n 'shah|root|apache' passwd_file.gz
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
36:shaha:x:500:501:Shaha Alam:/home/shaha:/bin/bash
38:apache:x:48:48:Apache:/var/www:/sbin/nologin
[shaha@oc8535558703 Desktop]$

c)列出使用-l选项包含多个模式的压缩文件的文件名。

[shaha@oc8535558703 Desktop]$zegrep -l 'shah|root|apache' *.gz
passwd_file1.gz
passwd_file2.gz
passwd_file3.gz
passwd_file.gz
[shaha@oc8535558703 Desktop]$

示例6:使用ZDIFF/ZCMP的文件比较

我们可以将两个压缩文件与Zdiff/zcmp进行比较,如下所示。
这将与未压缩的文件操作'diff file1 file2'相同。

[shaha@oc8535558703 Desktop]$cat > file1.txt
This is line one
This is line two
[shaha@oc8535558703 Desktop]$cat > file2.txt
This is line 1
This is line two
[shaha@oc8535558703 Desktop]$diff file1.txt file2.txt
1c1
This is line 1
[shaha@oc8535558703 Desktop]$gzip file1.txt file2.txt
[shaha@oc8535558703 Desktop]$zdiff file1.txt.gz file2.txt.gz
1c1
This is line 1