shell小技巧(四十六)删除文件指定行和行部分内容

把一个文档前五行中包含字母的行删掉,同时删除6到10行包含的所有字母
测试文件内容类似:

1fffff

2222222

34545-=-93

4aaaAa

555555~#$%

6dfd444

7545454dfsdf

8888888

9999999A

10erewr

11gdfgfd888

1256456dgfd
代码:
方法1:

#!/bin/bash

sum=$(cat ./txt/delchar.txt | wc -l)

echo total:${sum}

head -5 ./txt/delchar.txt | sed  '/[a-zA-Z]/d' > ./txt/delchar1.txt

head -10 ./txt/delchar.txt | tail -5 | sed  's/[a-zA-Z]//g' >> ./txt/delchar1.txt

let lastpart=sum-10

tail -${lastpart} ./txt/delchar.txt >> ./txt/delchar1.txt

方法2:

#!/bin/bash

> ./txt/delchar2.txt

i=1

while read line

do

if [ $i -le 5 ]; then

   echo $line |sed  '/[a-zA-Z]/d' >> ./txt/delchar2.txt

elif [ $i -le 10 ] && [ $i -gt 5 ]; then

    echo $line

    echo $line |sed  's/[a-zA-Z]//g' >> ./txt/delchar2.txt

else

    echo $line >> ./txt/delchar2.txt

fi

let i=i+1

done < ./txt/delchar.txt

发布了95 篇原创文章 · 获赞 3 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/bigwood99/article/details/105447074
今日推荐