格式化列印printf
printf '%s\t %s\t %s\t %s\t %s\t \n' $(cat printf.txt)
# %s代表不确定长度的字符串,\t代表 分隔符[tab]
printf '%10s %5i %5i %5i %8.2f \n' $(cat printf.txt |\
> grep -v Name)
# %10s 长度为5个字节的字符串栏位
# %5i 长度为5个字符串的数字栏位
# %8.2f 8个字节,具有小数点的栏位 8.2f--->00000.00
数据处理工具awk
sed–>操作一整行
awk–>处理每一行栏位内的数据(以tab或空格分割)
last -n 5 | awk '{print $1 "\t" $3}'
#取出第一列$1和第三列$3,并用tab分割.$0表示一整列
variable | meaning |
---|---|
NF | 每一行的列数(栏位) |
NR | 行数 |
FS | 目前的分隔字节 |
last -n 5| awk '{print $1 "\t lines: " NR "\t columns: " NF}'
#统计行列数输出
cat /etc/passwd | \
> awk 'BEGIN{FS=":"} $3 < 10 {print $1 "\t " $3}'
#因为文件中以','作为分隔符-->另FS=":";若要从第一列$1开始:BEGIN{FS..}
awk的选择计算
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}'
文件对比工具
diff
diff [-bBi] from-file to-file
#原始文件:from-file,对比文件: to_file
diff Path1 Path2
cmp
cmp [-s] file1 file2
patch
diff -Naur passwd.old passwd.new > passwd.patch