user3463055
user3463055

Reputation: 131

UNIX - delete specific lines

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

Upvotes: 0

Views: 67

Answers (1)

shellter
shellter

Reputation: 37258

# 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

Upvotes: 1

Related Questions