-1

If the following file contains same content in all line it should print "contents are same".

$ cat file
  example-line
  example-line
  example-line

If any of the line differs from others, then it should print "contents are different"

e.g.

$ cat file
  example-line
  somethingelse
  example-line

will print "contents are different".

1

Using awk. Map the key names in the array unique and by the time the entire file processing is complete, the array should have just one key.

awk '!unique[$0]++ { count++ } END { print count == 1 ? "contents are same" : "contents are different"  }' file

The general syntax for awk is a pattern-action model.

pattern { action }

The part !unique[$0]++ creates a hash map (associative array) by creating the key as the content of the whole line $0. When the first time an unique line is seen, the value for unique['<key-name>'] will be zero, and the ! block negates this false and becomes a true action for the {..} part where we increment a counter.

Since we also do a post-increment on the value, for subsequent occurrences of the same line, the negation will produce a boolean false condition causing the {..} block to be not executed. The instructions within END are executed after all the lines are processed. Since we just have one key in the array at the end of the processing, we declare the lines are same.

| improve this answer | |
  • It works as expected. if possible, can you explain this please? – smc Aug 5 '19 at 7:37
  • Why is this downvoted? When it has a well explained content? – Inian Aug 5 '19 at 9:16
  • Thanks for the explanation, I am not sure who downvoted this, this awk script works great to me. – smc Aug 5 '19 at 9:35
  • @smc When you post an incomplete (e.g.one without an attempted solution like yours) it usually gets downvotes and often the answers get downvoted too for "encouraging" people to post incomplete questions. So idk but maybe it's that? – Ed Morton Aug 6 '19 at 15:24
4

Using uniq:

uniq -c testfile | [ $(wc -l) -eq 1 ] && echo 'contents are the same' || echo 'contents are different'

| improve this answer | |
  • This. The core unix tools are very powerful and may save time. – Thorbjørn Ravn Andersen Aug 5 '19 at 7:58
  • Note that this would print ... different if the file can't be found or can't be read. – Kusalananda Aug 5 '19 at 8:00
1

Tried with below awk solution and it worked fine

command

i=`awk '{if (!seen[$0]++){print $0}}' filename| wc -l`



if [[ $i == 1 ]]; then echo "contents are same"; else echo "contents are different"; fi
| improve this answer | |
1

All you need to do the comparison concisely, portably and efficiently is:

awk '!seen[$0]++ && NR>1{exit 1}'

For example and with output messages added:

$ cat file1
  example-line
  example-line
  example-line

$ cat file2
  example-line
  somethingelse
  example-line

$ awk '!seen[$0]++ && NR>1{f=1; exit} END{print "contents are " (f ? "different" : "same"); exit f}' file1
contents are same
$ echo $?
0

$ awk '!seen[$0]++ && NR>1{f=1; exit} END{print "contents are " (f ? "different" : "same"); exit f}' file2
contents are different
$ echo $?
1
| improve this answer | |

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.