首页 > 试题广场 >

打印只有一个数字的行

[编程题]打印只有一个数字的行
  • 热度指数:13721 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
假设有一个nowcoder.txt,编写脚本,打印只有一个数字的行。

假设nowcoder.txt内容如下:
haha
1
2ab
cd
77
那么你的脚本应该输出
1
2ab

输入描述:
1


输出描述:
1
示例1

输入

haha
1
2ab
cd
77

输出

1
2ab
while read line; do
    out=${line//[^0-9]/}  # 进行字符串替换,去除非数字字符

    if [ ${#out} -eq 1 ]; then # 计算数字字符长度并输出
        echo $line 
    fi
done < nowcoder.txt
发表于 2022-04-26 15:53:27 回复(0)
awk -F "[0-9]" '{if(NF==2) print $0}' nowcoder.txt

发表于 2021-12-09 12:18:31 回复(9)
纯shell
while read line
do
    count=0
    for ((i=0;i<${#line};i++))
        do
            [[ ${line:i:1} =~ [0-9] ]] && ((count++))
        done
    if [ $count -eq 1 ];then
        printf "$line\n"
    fi
done < nowcoder.txt
纯awk
awk -F "" '{
    for (i=1; i<=NF; i++) {
        if ($i ~ /[0-9]/) {
            k++
        }
    }
    if (k==1){print($0)}
    k=0
}'



发表于 2021-11-25 11:24:10 回复(0)
grep -E '^[a-Z]*[0-9][a-Z]*$' nowcoder.txt

发表于 2022-05-19 14:08:24 回复(0)
grep -P '^\D*\d\D*$'
发表于 2021-11-19 20:29:33 回复(0)
awk '/^[a-z]*[0-9][a-z]*$/ {print $0}'
发表于 2022-12-09 16:21:22 回复(0)
awk '$0~/^[^0-9]*[0-9][^0-9]*$/{print $0}'
发表于 2023-04-13 15:00:00 回复(0)
cat nowcoder.txt|awk -F "" '{
  for(i=1;i<=NF;i++)
  {
    if($i ~ /[0-9]/)
    {
      count++   
    }
  }
  if(count==1)
  {
    print $0  #输出对应的行
  }
  count=0
}'
发表于 2022-03-21 19:58:30 回复(0)
grep -Px '\D*\d\D*'

发表于 2025-05-08 13:25:49 回复(0)
各位大佬,为什么以下结果为2呢?
[root@linuxserver ~]# echo "1" |awk -F "[0-9]" '{print NF}'
2
发表于 2024-10-23 17:22:54 回复(0)
file="nowcoder.txt"
while read line
do
   num_detail=`echo "$line" | sed 's/[^0-9]//g'`  
   if [ ${#num_detail} -eq 1 ]; then
    echo "$line"
   fi
done < $file
发表于 2024-07-03 22:33:59 回复(0)
#!/bin/bash

while IFS= read -r line; do
	temp=$(echo "$line" | tr -dc "0-9")
	if [ ${#temp} -eq 1 ]; then
		echo "$line"
	fi
done <nowcoder.txt

发表于 2024-07-02 17:31:17 回复(0)
cat nowcoder.txt|awk '
    {
        sum=0
        for(i=1;i<=length($0);i++){
            if(substr($0,i,1)>=0&&substr($0,i,1)<=9){
                sum++
            }
        }
        if(sum==1){
            print $0
        }
    }
'
发表于 2024-06-30 16:15:44 回复(0)
awk '{if($0 ~ /^[^0-9]*[0-9][^0-9]*$/) printf("%s\n",$0)}'
发表于 2023-09-22 16:32:56 回复(0)
awk '/^[0-9]$|[0-9][[:alpha:]]/{print $0}'

发表于 2023-08-21 15:33:32 回复(0)
# 方法1:使用while read line
while read -r line; do
    # 将每行的数字的过滤出来,并得出数量
    num=$(echo "${line}" | grep -o [0-9] | wc -l)
    # 如果数量不是1则continue
    [[ ${num} -ne 1 ]] && continue
    echo "$line"
done < nowcoder.txt

# 方法2: 使用awk
# 使用awk逐行遍历,在每行处理中逐个字符处理,
awk -F "" 'BEGIN{}{
    k=0;
    for(i=1;i<=NF;i++){
        if($i ~ /[0-9]/) {
            k++
        }
    }
    if(k==1) {
        print $0
    }
}END{}' nowcoder.txt

# 因为用不到BEGIN和END,则可以选择继续进化
awk -F "" '{
    k=0;
    for(i=1;i<=NF;i++){
        if($i ~ /[0-9]/) {
            k++
        }
    }
    if(k==1) {
        print $0
    }
}' nowcoder.txt

# 发现上面的复杂度还是挺高的,继续优化, 通过if(k>=2)降低遍历次数降低复杂度
awk -F "" '{
    k=0;
    for(i=1;i<=NF;i++){
        if($i ~ /[0-9]/) {
            k++
        }
        if(k>=2){
            next
        }
    }
    if(k==1) {
        print $0
    }
}' nowcoder.txt

发表于 2023-03-21 13:50:08 回复(0)
#!/bin/bash

while read -r line
do
sum=$(echo $line|grep -Eo '[0-9]'|wc -l)
if [ $sum -eq 1 ];then
echo $line
fi
done < nowcoder.txt
发表于 2023-02-08 16:31:16 回复(0)
grep -E '^[^0-9]*[0-9][^0-9]*$' nowcoder.txt

发表于 2023-01-28 23:58:08 回复(0)
awk '/(^[0-9][a-z]*$) |^[a-z]*[0-9][a-z]*$/'
发表于 2023-01-17 19:27:50 回复(0)
while read line; do
    if [ `echo $line | grep -oE '[0-9]' | wc -l` -eq 1 ]; then
        echo $line
    fi
done

发表于 2023-01-07 14:20:13 回复(0)