需求以下:shell
用shell實現,把一個文件文檔中只有一個數字的行給打印出來。(以/password文件爲例,自行修改)bash
參考解答以下code
#!/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
#!/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
#!/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