So,I have to write a bash script to check if a 9x9 "sudoku" solution is valid,but the simplification is that I don't have to divide it into 3x3,but just check if rows and columns contain any duplicate numbers,and valid numbers are only 1-9..
this is what I had in mind,but couldn't get it working:
#!/bin/bash
error="false"
count=0
#this would be for columns
#for i in 1 2 3 4 5 6 7 8 9
#do
#cat sudoku.txt | awk -F "" '{ print $'$i'}' | uniq -c | awk '$1 > 1 { count++ } END { print count }'
#done
#and this would be for rows
for i in 1 2 3 4 5 6 7 8 9
do
cat sudoku.txt | awk '{ print FNR=$'$i'}' | uniq -c |
awk '$1 > 1 { count++ } END { print count }' |
awk ' count > 0 { $error="true" } END { print $i }' |
awk '{ if ($error = "true") print "Wrong data!"; else print "Correct data!"; } '
done
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…