Bash解析素数列表。
我想解析一个从文件中获取的hexa列表,在解析它们以获得唯一的hexa并对它们进行排序之后 列表如下所示:Bash解析素数列表。,bash,parsing,Bash,Parsing,我想解析一个从文件中获取的hexa列表,在解析它们以获得唯一的hexa并对它们进行排序之后 列表如下所示: 1 0xb6e38000 8 0xb6f66000 5 0xb6f69000 1 0xb6f6c000 3 0xb6fd4000 1 0xb6ff7000 2 0xb6ff8000 4 0xb6ffa000 1 0xb6ffb000 现在我想做的是进一步细化它,这样我只得到前面有素数的六边形,如下所示: 1 0xb6e38000
1 0xb6e38000
8 0xb6f66000
5 0xb6f69000
1 0xb6f6c000
3 0xb6fd4000
1 0xb6ff7000
2 0xb6ff8000
4 0xb6ffa000
1 0xb6ffb000
现在我想做的是进一步细化它,这样我只得到前面有素数的六边形,如下所示:
1 0xb6e38000
5 0xb6f69000
1 0xb6f6c000
3 0xb6fd4000
1 0xb6ff7000
1 0xb6ffb000
我一直使用的命令是:
sort | uniq -c | grep " 1 0x"
但此命令仅列出在文件中仅出现一次的文件。
有人能帮我“整理”一下吗?素数的原始答案
如问题注释中所述,1
不是,但在所需输出的描述中,您列出了以1
开头的行。但是,如果您确实希望过滤掉第一列中带有素数的行,那么以下脚本将有所帮助:
#!/bin/bash -
[ $# -gt 0 ] && source_file="$1"
: ${source_file:=/tmp/source-file}
function is_prime {
declare -i n="$1"
if [ $n -le 1 ]; then
return 1
elif [ $n -le 3 ]; then
return 0
elif [[ $(( $n % 2 )) == 0 || $(( $n % 3 )) == 0 ]]; then
return 1
fi
i=5
while [[ $(( $i * $i )) -le $n ]]; do
if [[ $(( $n % $i )) == 0 || $(( $n % ($i + 2) )) == 0 ]]; then
return 1
fi
(( i += 6 ))
done
return 0
}
while read -a line; do
# We accept only two or more columns
[ ${#line[@]} -ge 2 ] || continue;
if is_prime ${line[0]}; then
echo $line
else
echo >&2 "skipping ${line[*]} as ${line[0]} is not prime"
fi
done < "$source_file"
错误
奇数更新
我应该从一开始就提到这一点,我需要奇数行
出现,而不是质数。我的错生物的
很容易检测一个数字是否是奇数,表达式类似于if[[$($n%2))!=0]
。表达式检查2
的余数是否不等于零,即应用。如果余数是零,那么这个数字是偶数,当然是奇数
全文如下:
#!/bin/bash -
[ $# -gt 0 ] && source_file="$1"
: ${source_file:=/tmp/source-file}
while read -a line; do
# We accept only two or more columns
[ ${#line[@]} -ge 2 ] || continue;
declare -i n=${line[0]}
if [[ $(( $n % 2 )) != 0 ]]; then
echo ${line[*]}
else
echo >&2 "skipping ${line[*]} as ${line[0]} is even"
fi
done < "$source_file"
错误
第一列中可能出现的素数是什么?为什么
1
是素数而2
不是?@伊尼安,同意,因此询问第1列中可能出现的素数,以及OP对素数的定义:)@Sundeep:也许OP必须重新表述prime
部分,就像你说的,也许他只是想输入奇数,因为1
不是prime@Inian是的,OP也需要用重复的十六进制值显示输入样本,否则就不需要使用sort/uniq…“…我需要奇数…我的错”请更新您的问题和标题!好悲伤。我看到了我将给你一个我使用的完整命令的例子:cat${parcer}u brut.txt | grep“map”>${parcer}u parced.txt
sed-e's/\(.*)\(0x……。\)\(.\)/\2/'${parcer u parced.txt |排序| uniq-c | grep“0x”| tr d'[:blank:]“| cut-c2->exclude.lst
关于如何将您的解决方案集成到此中有什么想法吗?@biotic,我不知道您为什么添加了| cut-c2-
,但其他内容似乎生成了您在原始问题中提到的列表。然后您可以将结果重定向到某个文件,然后启动bash script.sh/path/to/source file>filtered 2>errors
。另一种方法是添加另一个管道:|在读取时-一行;做完成此答案中脚本中的操作(不要使用<“$source\u file”
)。简化示例:您的命令…|读的时候——一行;do[[$(${line[0]}%2))!=0]]和&echo${line[*]};完成
问题是我现在已经尝试了一些方法,但是列表生成命令似乎有一些问题。
skipping 1 0xb6e38000 as 1 is not prime
skipping 8 0xb6f66000 as 8 is not prime
skipping 1 0xb6f6c000 as 1 is not prime
skipping 1 0xb6ff7000 as 1 is not prime
skipping 4 0xb6ffa000 as 4 is not prime
skipping 1 0xb6ffb000 as 1 is not prime
#!/bin/bash -
[ $# -gt 0 ] && source_file="$1"
: ${source_file:=/tmp/source-file}
while read -a line; do
# We accept only two or more columns
[ ${#line[@]} -ge 2 ] || continue;
declare -i n=${line[0]}
if [[ $(( $n % 2 )) != 0 ]]; then
echo ${line[*]}
else
echo >&2 "skipping ${line[*]} as ${line[0]} is even"
fi
done < "$source_file"
1 0xb6e38000
5 0xb6f69000
1 0xb6f6c000
3 0xb6fd4000
1 0xb6ff7000
1 0xb6ffb000
skipping 8 0xb6f66000 as 8 is even
skipping 2 0xb6ff8000 as 2 is even
skipping 4 0xb6ffa000 as 4 is even