您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
二周 发布于2020年08月11日 12:05

原创 Linux 日志处理-高效命令

2227 次浏览 读完需要≈ 5 分钟 LinuxShell

内容目录

多文件过滤统计

比如下面在所有的 *.log 中过滤 APPSEARCHLOG

$ for f in `ls *.log`;do grep APPSEARCHLOG $f|echo $f `wc -l`;done

app.2020-08-06.log 126393
app.2020-08-08.log 47788
app.2020-08-09.log 42622
app.2020-08-10.log 42621
app.log 9504

GBK转UTF8编码

在Windows上生成的txt文本默认是GBK编码的,用代码编辑器打开中文乱码,用记事本打开就OK。

一般可以手动选择记事本打开,然后另存为,选择编码为UTF8。但是如果文件太多就很麻烦的。

# 单个文件转换
$ iconv --verbose -f gbk -t utf-8 gbk.log > utf8.log
gbk.log:

# 查看当前目录下的gbk编码的文件
$ ls
gbk.1.log  gbk.2.log  gbk.3.log
# 批量转转,utf8编码的文件就在原文件名后面加上“.utf8.log”
$ for f in `ls *.log`;do iconv --verbose -f gbk -t utf-8 $f > $f.utf8.log ;done
gbk.1.log:
gbk.2.log:
gbk.3.log:

# 查看转换结果
$ ls
gbk.1.log  gbk.1.log.utf8.log  gbk.2.log  gbk.2.log.utf8.log  gbk.3.log  gbk.3.log.utf8.log

# 验证转换结果: 使用file,命令
$ file *.log
gbk.1.log:          ISO-8859 text, with CRLF line terminators
gbk.1.log.utf8.log: UTF-8 Unicode text, with CRLF line terminators
gbk.2.log:          ISO-8859 text, with CRLF line terminators
gbk.2.log.utf8.log: UTF-8 Unicode text, with CRLF line terminators
gbk.3.log:          ISO-8859 text, with CRLF line terminators
gbk.3.log.utf8.log: UTF-8 Unicode text, with CRLF line terminators

dos2unix

Windows上的文件换行符是 \r\n,Unix上的都是 \n. 为了文件的统一,可以一键转换为Unix文件格式,使用 dos2unix 命令

$ dos2unix *.log
dos2unix: converting file gbk.1.log to Unix format...
dos2unix: converting file gbk.1.log.utf8.log to Unix format...
dos2unix: converting file gbk.2.log to Unix format...
dos2unix: converting file gbk.2.log.utf8.log to Unix format...
dos2unix: converting file gbk.3.log to Unix format...
dos2unix: converting file gbk.3.log.utf8.log to Unix format...

# 验证下转换结果
$ file *.log
gbk.1.log:          ISO-8859 text
gbk.1.log.utf8.log: UTF-8 Unicode text
gbk.2.log:          ISO-8859 text
gbk.2.log.utf8.log: UTF-8 Unicode text
gbk.3.log:          ISO-8859 text
gbk.3.log.utf8.log: UTF-8 Unicode text
  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论