linux格式化文本输出
程序员文章站
2022-07-14 23:36:46
...
Linux文本输出没有标准化的格式,不容易观看,今天研究了下,用awk下面格式;
awk '{printf "%-18s%-8s%-8s%-8s%-55s%-30s%-50s\n",$1,$2,$3,$4,$5,$6,$7}' cpu.log > cpu_$tsSuf.log
shell中如果要修改源文件的话,可以先导出到一个临时文件,然后格式化输出后再导出到指定文件
在shell最后将临时文件删除即可;
今天写了个脚本,生成巡检的文本;每天定时导出到文件;
#!/bin/bash
#append department
ts=$(date -d '24 hours ago' +'%Y-%m-%d %H:%M:%S')
tsSuf=$(date -d '24 hours ago' +'%Y_%m_%d')
YearM=$(date +'%Y%m')
mysql --login-path=localhost -e "select h.ip,m.disk,count(*) as times,h.role,h.domain,h.erp,h.organizationFullName from jd_dbmonitor.db_host as h, jd_dbmonitor.machine_data_$YearM as m where h.id=m.host_id and disk > 80 and m.add_time > '$ts' and h.role='M' and h.db_type<>1 group by h.ip ;" > disk.log
awk '{printf "%-18s%-8s%-8s%-8s%-55s%-30s%-50s\n",$1,$2,$3,$4,$5,$6,$7}' disk.log > disk_$tsSuf.log
mysql --login-path=localhost -e "select h.ip,m.diskio,count(*) as times,h.role,h.domain,h.erp,h.organizationFullName from jd_dbmonitor.db_host as h, jd_dbmonitor.machine_data_$YearM as m where h.id=m.host_idand diskio > 50 and h.role='M' and h.db_type<>1 group by h.ip order by times desc;" > io.log
awk '{printf "%-18s%-8s%-8s%-8s%-55s%-30s%-50s\n",$1,$2,$3,$4,$5,$6,$7}' io.log > io_$tsSuf.log
mysql --login-path=localhost -e "select h.ip,m.cpu,count(*) as times,h.role,h.domain,h.erp,h.organizationFullName from jd_dbmonitor.db_host as h, jd_dbmonitor.machine_data_$YearM as m where h.id=m.host_id andcpu > 90 and h.role='M' and h.db_type<>1 group by h.ip order by times desc;" > cpu.log
awk '{printf "%-18s%-8s%-8s%-8s%-55s%-30s%-50s\n",$1,$2,$3,$4,$5,$6,$7}' cpu.log > cpu_$tsSuf.log
/bin/rm cpu.log io.log disk.log
echo "$ts cpu/disk/io has output success."