Score:0

Is posible to write script that checks for differences between two patterns in configuration files

re flag

Linux. I have for example two config files. First:

    criticalexceptions => [
    'yum-updatesd-helper: error',
    'cups: cupsd shutdown succeeded',
    'hda: drive_cmd: error',
    'end_request: I\/O error, dev fd0',
    'SAP[A-Z0-9]{3}_[0-9]{2}\[[0-9]+\]',
    'nrpe\[[0-9]+\]:',
    'collectd\[[0-9]+\]:',
    'kernel: cdrom: open failed',
    'gconfd.*Failed',
    'nrpe.*ERROR.*seteuid(0): Operation not permitted',
],
criticalpatterns => [
    'kernel: Kernel BUG',
    'error',
    'critical',
    'failed',
    'warning',
    'HACMP',
    'shutdown',
    'panic',
    'exiting',
],

and second

    criticalexceptions => [
    'as-ncs01 logger:',
    'logger: NetWorker media:',
    'warning while saving',
    'could not be opened and was not backed up',
    'NetWorker',
    'zmd:',
    'Succeeded with warning',
    'failed - POSSIBLE BREAKIN ATTEMPT!',
    'JBD: barrier-based sync failed on',
    'cmaeventd\[.*\]\: Logical drive [0-9] of Array Controller in slot',
    'end_request: I\/O error, dev fd0',
    'logrotate: error: unable to open \/var\/log\/sudosh',
    'logrotate: error: failed to rename \/var\/log\/sudosh',
    'smbd',
    'sap2.* winbindd\[\d{0,}\]\:   cli_rpc_pipe_open_schannel\: failed to get schannel session',
    'suse_register\[\d+\]:',
    'SAP[a-zA-Z0-9#]{3}_[0-9#]{2}\[[0-9]+\]',
    'nrpe\[[0-9]+\]:',
    'collectd\[[0-9]+\]:',
    'kernel: cdrom: open failed',
    'smartd\[[0-9]+\]:',
    'modprobe:.+Error inserting floppy',
    'saposcol\[[0-9]+\]:.+segfault at',
    'sapccm4x\[[0-9]+\]:.+segfault at',
    'XMLForm\[[0-9]+\]:',
    'sapxpg\[[0-9]+\]:',
    '\[Hardware Error\]: Machine check events logged',
    'snowagent:.+Could not parse time format', #9001208224

],
criticalpatterns => [
    'kernel: Kernel BUG',
    'error',
    'critical',
    'failed',
    'warning',
    'HACMP',
    'shutdown',
    'panic',
    'exiting',
    'link status definitely',
    'Link is (Down|Up)',
],

Is posible to write script that checks for differences between two patterns in this two configuration files? Screen with example

in flag
Is the order of strings important?
Boniek avatar
re flag
I think not, is not important. Do You have any ideas?
in flag
Yes, sort the strings for each section and use `diff` or `comm`.
Cyrus avatar
in flag
Please edit your question and add your desired output (no description) for that sample input.
Score:0
gb flag

You've already got a side-by-side diff and it's unclear what you want the script to do.

Diffs aren't well equipped to deal with data like this, so the first thing you'll need to do is flatten it in a manner that makes each line descriptive of the structure. Then you can sort it and diff it.

flatten() {
  echo "$*"
  awk '
    /^\]/ { arr = "" }
    arr != "" && NF { sub(/^[[:space:]]+/, ""); print arr $0 }
    $2 $3 == "=>[" { arr = $1 $2 }
  ' "$@" |sort
}

gvimdiff <(flatten /tmp/a) <(flatten /tmp/b)

I've defined a flatten function that uses awk to flatten the data in your files. To remember what file we're on, the function prints the parameters (the file) at the top. Then we run awk. For lines that start with ], we've closed a structure. Empty the arr variable. If there is a non-empty arr variable and the line isn't blank (the number of fields is not zero), remove the leading white space and print it prefixed with the arr variable's contents. If the second and third fields concatenate to =>[ then we've started an array. Save the array name and the => in the arr variable. We then sort the output.

Now we can do a more informative diff. Rather than using temporary files, I'm using a bash technique to pipe the output of two flatten commands directly into diff.

I've run gvimdiff because that's my own go-to:

gvimdiff output

You could also run diff -u <(flatten /tmp/a) <(flatten /tmp/b) |grep '^-' to get your removed lines, etc.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.