Linux学习:文件的格式化与相关处理(第11章)

1 格式化打印:printf

printf '打印格式' 实际内容
选项与参数:
关于格式方面的几个特殊样式:
       \a    警告声音输出
       \b    倒退键(backspace)
       \f    清除屏幕 (form feed)
       \n    输出新的一行
       \r    亦即 Enter 按键
       \t    水平的 [tab] 按键
       \v    垂直的 [tab] 按键
       \xNN  NN 为两位数的数字,可以转换数字成为字符。
关于 C 程序语言内,常见的变量格式
       %ns   那个 n 是数字, s 代表 string ,亦即多少个字符;
       %ni   那个 n 是数字, i 代表 integer ,亦即多少整数码数;
       %N.nf 那个 n 与 N 都是数字, f 代表 floating (浮点),如果有小数码数,
             假设我共要十个位数,但小数点有两位,即为 %10.2f 
[mcb@localhost ~]$ printf '%10s %5i %5i %5i %8.2f \n' $(cat print.txt | grep -v Name)
    DmTsai    80    60    92    77.33
     VBird    75    55    80    70.00
       Ken    60    90    70    73.33

2 awk:数据处理工具

 awk 比较倾向于一行当中分成数个“字段”来处理。适合处理小型的数据数据处理

[mcb@localhost ~]$ awk '条件类型1{动作1} 条件类型2{动作2} ...' filename
取出帐号与登陆者的 IP ,且帐号与 IP 之间以 [tab] 隔开
[mcb@localhost ~]$ last -n 5 | awk '{print $1 "\t" $3}'
mcb     192.168.64.1
mcb     Mon
mcb     :0
reboot  boot
mcb     192.168.64.1

 awk 的内置变量

变量名称代表意义
NF每一行($0)拥有的字段总数
NR目前 awk 所处理的是“第几行”数据
FS目前的分隔字符,默认是空白键
[mcb@localhost ~]$ last -n 5 | awk '{print $1 "\t lines: " NR "\t columns: " NF}'
mcb      lines: 1        columns: 10
mcb      lines: 2        columns: 9
mcb      lines: 3        columns: 10
reboot   lines: 4        columns: 11
mcb      lines: 5        columns: 10
[mcb@localhost ~]$ cat /etc/passwd | awk '{FS=":"} $3<10 {print $1 "\t" $3}'
root:x:0:0:root:/root:/bin/bash
bin     1
daemon  2
adm     3
lp      4
sync    5
.....
[mcb@localhost ~]$ cat /etc/passwd | awk 'BEGIN {FS=":"} $3<10 {print $1 "\t" $3}'
root    0
bin     1
daemon  2
adm     3
......

对于一个数据表,如何计算每个人的总额并格式化输出?我们可以这样考虑:

  • 第一行只是说明,所以第一行不要进行加总 (NR==1 时处理);
  • 第二行以后就会有加总的情况出现 (NR>=2 以后处理)
[mcb@localhost ~]$ cat pay.txt | \
> awk 'NR==1{printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4."Total"}
> NR>=2{total=$2+$3+$4
> printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}'
      Name       1st        2nd        3th     Total
     VBird      23000      24000      25000   72000.00
    DMTsai      21000      20000      23000   64000.00
     Bird2      43000      42000      41000  126000.00

3 文件比对工具

3.1 diff  用于比对两个文件之间的差异

diff是以行为单位来进行比较,一般用于ascll纯文本文件的比对上,由于是以行为比对的单位,因此 diff 通常是用在同一的文件(或软件)的新旧版本差异上

diff [-bBi] from-file to-file
选项与参数:
from-file :一个文件名,作为原始比对文件的文件名;
to-file   :一个文件名,作为目的比对文件的文件名;
注意,from-file 或 to-file 可以 - 取代,那个 - 代表“Standard input”之意。

-b  :忽略一行当中,仅有多个空白的差异(例如 "about me" 与 "about     me" 视为相同
-B  :忽略空白行的差异。
-i  :忽略大小写的不同。

3.2 cmp

cmp主要利用“字节”单位去比对, 因此,当然也可以比对二进制文件 

cmp [-l] file1 file2
选项与参数:
-l  :将所有的不同点的字节处都列出来。因为 cmp 默认仅会输出第一个发现的不同点。

范例一:用 cmp 比较一下 passwd.old 及 passwd.new
[[email protected] testpw]$ cmp passwd.old passwd.new
passwd.old passwd.new differ: char 106, line 4

3.3 patch 将旧的文件升级成为新的文件

4 文件打印设置

可以同时选择与设置每一页打印时的标头与页码等 

打印 /etc/man_db.conf 

[mcb@localhost ~]$ pr /etc/man_db.conf


2018-10-31 04:26                 /etc/man_db.conf                 Page 1
//上一行就是使用pr处理后加的标题,标题中会有文件时间,文件文件名和页码三个。

#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
......下面省略

2018-10-31 04:26                 /etc/man_db.conf                 Page 2


# location of catpaths and the creation of database caches; it has no effect
# on privileges.
.......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值