简体   繁体   English

bash脚本:从每行中删除前缀后如何区分两个文件?

[英]bash script: how to diff two files after stripping prefix from each line?

I have two log files. 我有两个日志文件。 Each line is formatted as follows: 每行的格式如下:

<timestamp><rest of line>

with this timestamp format: 具有以下时间戳格式:

2015-10-06 04:35:55.909 REST OF LINE

I need to diff the two files modulo the timestamps, ie I need to compare lines of the two files without their timestamps. 我需要以时间戳为模来区分两个文件,即,我需要比较两个文件中没有时间戳的行。 What linux tools should I use? 我应该使用哪些Linux工具?

I am on a RedHat 6 machine running bash if it makes a difference 我在运行bash的RedHat 6机器上运行,如果有所作为

您不需要创建临时文件:使用bash 进程替换

diff <(cut -d" " -f3- log1) <(cut -d" " -f3- log2)

I would first generate the two files to compare with the header removed using the cut command like this : 我将首先生成两个文件,以与使用cut命令删除的标头进行比较,如下所示:

cut -f 3- -d " " file_to_compare > cut_file

And then use the diff command. 然后使用diff命令。

You can use 'cut' 您可以使用“剪切”

cat file1 | cut -b23- > file1cut
cat file2 | cut -b23- > file2cut

diff file1 file2

To print all fields but the first two the awk utility (and programming language) can be used: 要打印除前两个字段以外的所有字段,可以使用awk实用程序(和编程语言):

awk '{$1=$2=""; print $0}' file1 > newfile1

awk '{$1=$2=""; print $0}' file2 > newfile2

diff newfile1 newfile2

Well, as your're looking for a tool why not just use a Kompare. 好吧,当您在寻找工具时,为什么不只使用Kompare。 Its very powerful and well known which is used by most developers who uses Linux. 它非常强大且众所周知,大多数使用Linux的开发人员都在使用它。

https://www.kde.org/applications/development/kompare/ https://docs.kde.org/trunk5/en/kdesdk/kompare/using.html https://www.kde.org/applications/development/kompare/ https://docs.kde.org/trunk5/en/kdesdk/kompare/using.html

Kompare is a GUI front-end program that enables differences between source files to be viewed and merged. Kompare是一个GUI前端程序,可以查看和合并源文件之间的差异。 It can be used to compare differences on files or the contents of folders, and it supports a variety of diff formats and provide many options to customize the information level displayed. 它可用于比较文件或文件夹内容上的差异,并且支持多种diff格式,并提供许多选项以自定义显示的信息级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM