2

I have two files in unix, I need the difference between them. Simple enough diff will do. But I also need the name of the section that was edited, which is the name of the group.

File 1:

GROUPNAME = {"hostnames","morehostnames","alotreally",
"almost200","hosts","separated"
,"ingroupsof3"};

File 2:

GROUPNAME = {"hostnames","morehostnames","alotreally",
"almost200","hosts","separated"
! ,"ingroupsof3","addedthis"};

Sofar I have this:

diff -C 150 file1 file2 | awk '/= {/,/!/p' >> diff.txt

Since I don't know how many hosts are in a group, I can't use exact numbers, only patterns. diff shows me the 150 lines as requested, the changes between the two are highlighted with "!".

With the command above I get the start of the group:

GROUPNAME = {"hostnames","morehostnames","alotreally",

but it stops at the first line feed. I would like to get the whole group including the lines marked with '!' (which would be the change itself).

I tried sed, gave me roughly the same result. Thanks in advance!

  • Is `
    ` part of the file or do you just want to indicate a new line? If the latter, remove the `
    `
    – kvantour Apr 10 '18 at 08:25
  • you have an extra `p` in the command you tried.. see if `diff -C 150 file1 file2 | awk '/= {/,/!/'` is what you want.. equivalent sed would be `sed -n '/= {/,/!/p'` ... see also [How to select lines between two patterns?](https://stackoverflow.com/questions/38972736/how-to-select-lines-between-two-patterns) – Sundeep Apr 10 '18 at 08:44

1 Answers1

1

You just showed one line from each input file instead of showing us 2 input files that had some common and some differing lines with the associated expected output so you haven't given us anything to test against but MAYBE something like this (untested of course) is what you're looking for:

awk -v RS=';' 'NR==FNR{a[$0]; next} !($0 in a)' file1 file2

To compare in order of records:

awk -v RS=';' 'NR==FNR{a[NR]=$0; next} $0 != a[FNR]' file1 file2
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    It's basically the same file. We use a tool to edit policy files. Logging the changes is currently done by simply running diff on the edited file against the original. After we edit the file the diff only shows the two lines next to the edited line, which is not enough for audit. I want to include the GROUPNAME as well. Your solution is actually perfect! Thank you very much! – Zoltán Benesch Apr 10 '18 at 09:05
  • @ZoltánBenesch This is very elegant! But take into account that this does not take the original record order into account. – kvantour Apr 10 '18 at 09:13
  • @kantour : Yes, I noted that but that's ok, I'll match the original to the output I get from this, which should be enough. – Zoltán Benesch Apr 10 '18 at 09:22
  • It would be trivial to compare in record order, I updated my answer to show that too. If that `!` in your second file is some kind of indicator that that record has been updated then obviously we could just print records from file2 that contain that flag and we don't need to compare against file1 at all. You may want to post a followup question as I don't think this one captured what you really are trying to do, it was more about how to implement something that you THINK you need to do. – Ed Morton Apr 10 '18 at 14:20