sed刪除指定行


待處理文本如1-8行文本

$cat a 
1
2
3
4
5
6
7
8

sed刪除連續指定行:如1-4行

$sed '1,4d' a
5
6
7
8

sed刪除不連續指定行:如第一行和第三行

$sed -e '1d' -e '3d' a
2
4
5
6
7
8

 一個例子:

我有一個list文件如下:

$cat list 
unknown_other_2     #文件名,我要從文件里抓取一些信息,下同,發現下圖里沒有有些沒有我想要的信息,test后面的數字代表這個list文件的行號
unknown_other_5   #我要刪除大小為0的行
unknown_other_1      
F002_other_1
unknown_other_14
t_unknown_other_4
t_unknown_other_6
t_unknown_other_4
t_unknown_other_1
t_unknown_other_4
t_unknown_other_12
t_unknown_other_4
t_unknown_other_13
t_unknown_other_10
t_unknown_other_12
t_unknown_other_4
t_unknown_other_42
t_unknown_other_16
t_unknown_other_27
t_unknown_other_14
t_unknown_other_31
t_unknown_other_12
t_unknown_other_10
t_unknown_other_13
t_unknown_other_11
t_unknown_other_22
t_unknown_other_21
t_unknown_other_45
t_unknown_other_42
t_unknown_other_4
t_unknown_other_33
t_unknown_other_1
t_unknown_other_12
t_unknown_other_14
t_unknown_other_1
t_unknown_other_31
t_unknown_other_4
t_unknown_other_11
t_unknown_other_11
t_unknown_other_3

test文件,test后面的數字對應上面list行號:

代碼如下:

ls -l |awk '{if ($5==0){print $9}}'|cut -d "t" -f3|xargs   -I {} echo -e "-e {}d"|xargs|xargs -I {} echo sed -i -e {} list|sh

#執行結果
#sed -i -e test10d -e test12d -e test14d -e test16d -e test2d -e test27d -e test3d -e test31d -e test32d -e test33d -e test35d -e test38d -e test39d -e test4d -e test40d -e test5d -e test9d list

#分解說明
#ls -l |awk '{if ($5==0){print $9}}'  列出大小為0的test文件
#cut -d "t" -f3  抓取后面的數字    用字符"t" 分割test文件,取第三列數字部分
#xargs -I {} echo -e "-e {}d"|xargs|xargs -I {} echo sed -i -e {} list  打印執行語句
#sh  執行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM