UNIX - 删除特定行 [英] UNIX - delete specific lines

查看:38
本文介绍了UNIX - 删除特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在变量 (1,3,8,9) 中有一个行列表.该列表显示了我需要从文本文件中删除哪些行.我可以使用哪个函数来删除特定的行号?

I have a list of lines in variable (1,3,8,9). That list shows which lines I need to delete from a text file. which function I can use to delete specific lines number?

非常感谢您的回复

推荐答案

# set -vx
lines2del="(1,3,8,9)"
sedCmds=${lines2del//,/d;}
sedCmds=${sedCmds/(/}
sedCmds=${sedCmds/)/}
sedCmds=${sedCmds}d
sed -i "$sedCmds" file

删除 set -vx 之前的 # 以查看每个 cmd 执行时的调试/跟踪.

Remove the # before set -vx to see the debug/trace for each cmd as it is executed.

如果您的数据周围没有 ( ) (parens),请修复 line2del 变量并删除第二个和第三个 sedCmds= 行.

If you don't really have ( ) (parens) around your data, fix the line2del variable and remove the second and third sedCmds= lines.

IHTH

这篇关于UNIX - 删除特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆