【发布时间】:2018-02-15 08:13:49
【问题描述】:
我正在编写一个脚本并想运行 bzip2,但只能获得压缩比、压缩大小和未压缩大小。
当我运行bzip2 filename 时,我得到:
test.txt: 5.769:1, 1.387 bits/byte, 82.67% saved, 48108 in, 8339 out.
我只想获取最后三个字段:
82.67% saved, 48108 in, 8339 out
我已经尝试使用 awk
bzip2 -v test.txt | awk '{print $1 $2 $3}' 以及
bzip2 -v test.txt | awk -F', ' '{print $1}'
但由于它是一个字符串并且分隔符的间距不均匀,我不知道该怎么做。我也想摆脱任何文本,只输出数字,像这样
82.67% 48108 8339
我必须尽可能简单。谢谢!
编辑:
bzip2 -v test.txt | cat -A 的输出:
test.txt: 0.788:1, 10.154 bits/byte, -26.92% saved, 52 in, 66 out.
脚本:
#!/bin/sh
# program2.sh
#Name of the file input
NAME=$1
#Uncompressed size of the file input
UNCOMPRESSED=$(du -h $NAME | awk '{print $1}')
#################################################
#Prompts name entry if no argument provided, or stores given argument as name
if [ $# -eq 0 ];
then
echo "Error: No file name provided. Please run the script with a filename argument."
echo ""
exit
fi
echo ""
echo "$NAME will be compressed using the gzip, bzip2, and zip commands."
echo ""
echo "gzip:"
#echoUncompressed:\t $UNCOMPRESSED"
gzip $NAME
gzip -l ${NAME%}.gz | awk ' NR == 2 {print "Uncompressed:\t " $2} NR == 2 {print "Compressed:\t " $1} NR == 2 {print "Ratio:\t\t " $3}'
gunzip ${NAME%}.gz
echo ""
echo "bzip2:"
echo "Uncompressed:\t $UNCOMPRESSED"
#Run bzip2
bzip2 -v $NAME |& awk -F ',[[:blank:]]*' '{sub(/\.$/, ""); printf "Ratio: %s, Uncompressed: %s, Compressed: %s\n", $(NF-2), $(NF-1), $NF}'
bunzip2 ${NAME%}.bz2
echo ""
echo "zip:"
#echoUncompressed:\t $UNCOMPRESSED"
#Run zip
zip -q ${NAME%.*}.zip $NAME
ZNAME="${NAME%.*}.zip"
unzip -ov $ZNAME | awk ' NR == 4 {print "Compressed:\t " $3} NR == 4 {print "Ratio:\t\t " $4}'
【问题讨论】: