1

Can I compare two text files skipping N symbols from start of the each line?

For example file1:

2018-05-31 12:00:00 This is the first line of text.
2018-05-31 12:00:00 This is the second line of text.
2018-05-31 12:00:00 This is the third line of text.
2018-05-31 12:00:00 This is the forth line of text.
2018-05-31 12:00:00 This is the fifth line of text.

and file2:

2018-05-31 12:00:01 This is the first line of text.
2018-05-31 12:00:02 This is the second line of text.
2018-05-31 12:00:03 This is the third line of text.
2018-05-31 12:00:04 This is the forth line of text.
2018-05-31 12:00:05 This is the fifth line of text.

If I compare two files line by line - they are different because of the seconds in time stamp.

But if I skip first 19 symbols from the start of each line in both files (date and time) - these files are identical. How to do that using shell command (script)?

Thank you very much in advance.

1

Using cut:

diff <(cut -c 20- file1) <(cut -c 20- file2)

Note: with GNU cut the -c character option actually works on bytes not characters, but this should be fine as long as your output starts with date/time stamps and not special characters.

  • If cut may be an issue, then use sed: sed 's/^.\{1,20\}//' file. – Isaac Jun 1 '18 at 7:21
  • 1
    Thank you very much, @Jesse_b - your solution works exactly the way I need! – pau Jun 1 '18 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.