简体   繁体   中英

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?

I am on a RedHat 6 machine running bash if it makes a difference

您不需要创建临时文件:使用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 -f 3- -d " " file_to_compare > cut_file

And then use the diff command.

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 '{$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. Its very powerful and well known which is used by most developers who uses Linux.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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