1. 程式人生 > >Shell練習(五)

Shell練習(五)

strong -c shell 打印 color 單詞 root 參考答案 sed

習題1:刪除文本中的字母

要求:把一個文本文檔的前5行中包含字母的行刪除掉,同時把6到10行中的全部字母刪除掉(文件1.txt,行數大於10行)

參考答案:

#!/bin/bash
#date:2018年2月9日
sfile="/root/2.txt"
N=`cat $sfile|wc -l`
for i in `seq 1 5`
do
   sed -n "$i"p $sfile|grep -v '[a-zA-Z]'
done
for i in `seq 6 10`
do
   sed -n "$i"p $sfile|sed 's/[a-zA-Z]//g'
done
for i in `seq 11 $N`
do
   sed -n "$i"p $sfile
done


習題2:查找字母數小於6的單詞

要求:用shell打印下面這句話中字母數小於6的單詞:Bash also interprets a number of multi-character options.

參考答案:

#!/bin/bash
# date: 2018年2月9日
str="Bash also interprets a number of multi-character options."
for work in $str
do
   len=`echo "$work"|wc -L`
   if [ $len -lt 6 ];then
        echo $work
   fi
done


Shell練習(五)