我试图仅使用awk
从bash中的字符串中找到最长的数字序列。我已经形成了以下命令,它正在给我输出。
$ echo "This_is_1234_and_44448888_1234567_111111_23456789_and_234" | sed 's/./\n&/g' | awk 'BEGIN{max_length=0} { tmp=match($1,/[0-9]/) ; if (tmp) { numbers[i]=numbers[i]$0;non_digit=0;} else if (non_digit<1) { non_digit=2 ;i++; } } END { i=0; for (key in numbers) { current_length=length(numbers[key]); if (current_length > max_length) { max_length = current_length; i = 0;} if (current_length >= max_length) {i++; max_length_strings[i] = numbers[key];} } print "max_length for the consecutive number portion is ",max_length; for ( j in max_length_strings ) { print "String_Part: " max_length_strings[j] " and Length: " max_length; }}'
max_length for the consecutive number portion is 8
String_Part: 23456789 and Length: 8
String_Part: 44448888 and Length: 8
但是为了从字符串中获取每个字符以进行数字检查,我使用sed
命令。
那么如何通过使用单个awk
命令来避免使用sed并获得相同的结果?
答案 0 :(得分:1)
只使用awk:
ambari-admin-password-reset
<强>解释强>
s="This_is_1234_and_44448888_1234567_111111_23456789_and_234"
awk -v RS='[^[:digit:]]+' 'length($0) >= max{
max=length($0)
num[max]=(num[max]?num[max] "," $0:$0)
}
END {
printf "max length=%s, numbers: %s\n", max, num[max]
}' <<< "$s"
max length=8, numbers: 44448888,23456789
我们将记录分隔符设为1个或多个非数字字符,因此每个记录都变为数字字段RS='[^[:digit:]]+'
块中的max
变量length($0) >= max
数组num
块中,我们只打印END
和max
数组条目num
gnu-awk
特定的
答案 1 :(得分:0)
使用GNU awk 4. * for FPAT和真正的多维数组:
$ cat tst.awk
BEGIN { FPAT="[0-9]+" }
{
delete strs
for (i=1;i<=NF;i++) {
cur = length($i)
strs[cur][$i]
max = (i>1 && cur>max ? cur : max)
}
for (str in strs[max]) {
printf "String_Part: %s and Length: %d\n", str, max
}
}
$ awk -f tst.awk file
String_Part: 23456789 and Length: 8
String_Part: 44448888 and Length: 8
以上假设您希望输出每个输入行的最大值而不是整个文件的输出。如果你确实想要它在整个文件中,那么再次使用GNU awk(对于RT):
$ cat tst.awk
BEGIN { RS="[0-9]+" }
{
cur = length(RT)
strs[cur][RT]
max = (NR>1 && cur>max ? cur : max)
}
END {
for (str in strs[max]) {
printf "String_Part: %s and Length: %d\n", str, max
}
}
$ awk -f tst.awk file
String_Part: 23456789 and Length: 8
String_Part: 44448888 and Length: 8