shell練習題5

需求以下:shell

用shell實現,把一個文件文檔中只有一個數字的行給打印出來。(以/password文件爲例,自行修改)bash

參考解答以下code

  • 方法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
相關文章
相關標籤/搜索