Pert8S
Pert8S

Reputation: 582

Validating file records shell script

I have a file with content as follows and want to validate the content as

1.I have entries of rec$NUM and this field should be repeated 7 times only. for example I have rec1.any_attribute this rec1 should come only 7 times in whole file.

2.I need validating script for this. If records for rec$NUM are less than 7 or Greater than 7 script should report that record.

FILE IS AS FOLLOWS :::

rec1:sourcefile.name=

rec1:mapfile.name=

rec1:outputfile.name=

rec1:logfile.name=

rec1:sourcefile.nodename_col=

rec1:sourcefle.snmpnode_col=

rec1:mapfile.enc=

rec2:sourcefile.name=abc

rec2:mapfile.name=

rec2:outputfile.name=

rec2:logfile.name=

rec2:sourcefile.nodename_col=

rec2:sourcefle.snmpnode_col=

rec2:mapfile.enc=

rec3:sourcefile.name=abc

rec3:mapfile.name=

rec3:outputfile.name=

rec3:logfile.name=

rec3:sourcefile.nodename_col=

rec3:sourcefle.snmpnode_col=

rec3:mapfile.enc=

Please Help

Thanks in Advance... :)

Upvotes: 0

Views: 102

Answers (3)

pasaba por aqui
pasaba por aqui

Reputation: 3539

The commands:

grep rec file2.txt | cut -d':' -f1 | uniq  -c | egrep -v '^ *7'

will success if file follows your rules, fails (and returns the failing record) if it doesn't.

(replace "uniq -c" by "sort -u" if record numbers can be mixed).

Upvotes: 0

anishsane
anishsane

Reputation: 20980

Simple awk:

awk -F: '/^rec/{a[$1]++}END{for(t in a){if(a[t]!=7){print "Some error for record: " t}}}' test.rc

Upvotes: 2

mainframer
mainframer

Reputation: 22139

grep '^rec1' file.txt | wc -l
grep '^rec2' file.txt | wc -l 
grep '^rec3' file.txt | wc -l

All above should return 7.

Upvotes: 0

Related Questions