可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have to write a script, which gets a text file which contains 9 line and 9 number (1-9) in the lines. For example:
123456789
234567891
345678912
456789123
567891234
678912345
789123456
891234567
912345678
I have to check lines and the rows based on the sudoku rules. Can't be same number in a line or in a row. So the example is a correct solution.
How to write it in bash shell script? I cannot use arrays, so what can I do?
回答1:
A solution without awk, combining different other tools.
EDIT: This solution works for an inputfile called input and spaces between the digits. See comments about changing this behaviour.
echo "Checking 9 lines" if [ $(wc -l <input ) -ne 9 ]; then echo "Wrong number of lines" exit 1 fi echo "Check for correct layout" if [ $(grep -cv '^[1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9]$' input ) -ne 0 ]; then echo "Not all lines are correct, maybe spaces at the end of a line?" grep -v '^[1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9]$' input exit 1 fi i=0 while read -r line; do ((i++)) if [ $(echo "${line}" | tr " " "\n" | sort -u | wc -l ) -ne 9 ]; then echo "Wrong nr of unique numbers in row $i" fi done <input for j in {1..9}; do if [ $(cut -d" " -f${j} input | sort -u | wc -l ) -ne 9 ]; then echo "Wrong nr of unique numbers in column $j" fi done