UNIX - delete specific lines

2019-09-10 12:45发布

问题:

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?

Thank you so much for your respond

回答1:

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

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

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

IHTH



标签: shell unix