问题描述
我必须写一个脚本,它可以获取其中包含9行和线9号(1-9)的文本文件。
例如:
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
234567891
345678912
345678912
456789123
456789123
567891234
567891234
678912345
678912345
789123456
789123456
891234567
891234567
912345678
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.
如何把它写在bash shell脚本?
我不能使用数组,所以我能做些什么?
How to write it in bash shell script?I cannot use arrays, so what can I do?
推荐答案
没有awk的一个解决方案,结合不同的其他工具。结果
编辑:此解决方案适用于数字之间的inputfile中称为输入和空格。请参阅有关更改此行为的意见。
A solution without awk, combining different other tools.
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
这篇关于的Bash shell脚本检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!