shell练习题5

需求如下:

用shell实现,把一个文件文档中只有一个数字的行给打印出来。(以/password文件为例,自行修改)

参考解答如下

  • 方法1
#!/bin/bash

file_name=passwd
num=$(awk -F: 'END{print NR}' $file_name)
for i in $(seq 1 $num)
do
    num=$(sed -n "$i"p $file_name | sed 's/[^0-9]//g' | wc -L)
        if [ $num -eq 1 ];then
        sed -n "$i"p $file_name
        fi
done
  • 方法2
#!/bin/bash

cat passwd | while read line
do
    num=$(echo $line | sed 's/[^0-9]//g' | wc -L)
    if [ $num -eq 1 ];then
        echo $line
    fi
done
  • 方法3
#!/bin/bash

file=/root/script/sh/passwd
while read line
do
    num=$(echo $line | sed 's/[^0-9]//g' | wc -L)
    if [ $num -eq 1 ];then
        echo $line
    fi
done <$file

猜你喜欢

转载自www.cnblogs.com/minn/p/9824598.html