Computing.Net > Forums > Unix > Deleting multiples lines in one SED

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Deleting multiples lines in one SED

Reply to Message Icon

Name: daelomin
Date: August 9, 2006 at 09:52:33 Pacific
OS: Linux
CPU/Ram: P4 1gig
Comment:

Hi,

I have figured out several line numbers that need to be deleted from a file.

Say lines_to_rm="8 9 10 11 34 35 78 79"

I have tried adding a "d" to each number & passing the "8d 9d 10d 11d" to sed in one go, it doesn't work.

From address ranges, I take it one can do consecutive lines like:

sed '8,9d' file

but apparently I can't do '8,9,14,15d'

any idea to do it in one line??

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: lchi2000g
Date: August 10, 2006 at 19:27:12 Pacific
Reply:

====> 1. input file

$ cat myfile.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

====> 2. script

$ cat 5.sh
#!/bin/ksh
set -x

DEL=d

cat <<EOF > sed.txt
8d
9d
10d
11d
34d
35d
78d
79d
EOF

sed -f sed.txt myfile.txt

====> run 5.sh script

$ ./5.sh
+ DEL=d
+ cat
+ << EOF
+ > sed.txt
+ sed -f sed.txt myfile.txt
1
2
3
4
5
6
7
12
13
14
15

Luke Chi


0

Response Number 2
Name: lchi2000g
Date: August 11, 2006 at 10:11:54 Pacific
Reply:

A better way:

sed '8d;9d;10d;11d;34d;35d;78d;79d' myfile.txt

Luke Chi


0

Response Number 3
Name: daelomin
Date: August 21, 2006 at 02:59:50 Pacific
Reply:

I knew there must be some character to separate. Just couldnt find it.

Thanks Luke!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Deleting multiples lines in one SED

Script for replacing multiple lines www.computing.net/answers/unix/script-for-replacing-multiple-lines/4145.html

Cancel Out lines in a file www.computing.net/answers/unix/cancel-out-lines-in-a-file/6765.html

Delete first line of a file www.computing.net/answers/unix/delete-first-line-of-a-file/7594.html