Delete multiple lines w/ sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete multiple lines w/ sed
# 1  
Old 07-24-2003
Data Delete multiple lines w/ sed

Hi all,

I am trying to figure out the syntx to delete multiple lines w/ sed. I know the following syntax will delete lines 1 THROUGH 5 from filex:

sed 1,5d filex

But I wan to delete lines 1 AND 5 (keeping lines 2,3, and 4). Does anyone know how to do this in a single sed statement?

THANKS!!
-bookoo
# 2  
Old 07-24-2003
If you are using sh/ksh/bash/zsh:
Code:
sed '1d
5d' filex

I.e., you need the line break.

The awk version is a bit more direct:
Code:
awk 'NR!=1 && NR!=5' filex

This takes advantage of the default awk action, which is to print the input line. This method simply accepts all lines except 1 and 5.

It can be generalized:
Code:
#!/bin/sh
while getopts s: arg; do
    case $arg in
    s)  skip="$OPTARG"
    esac
done
shift `expr $OPTIND - 1`
awk -v "sl=$skip" 'BEGIN {
        split(sl, a, ",")
        for (i in a) skip[a[i]] = 1
    }
!skip[NR]' "$1"




Last edited by criglerj; 07-25-2003 at 12:08 AM..
# 3  
Old 07-25-2003
Modern versions of sed support:
sed '1d;5d'

Even the very first version of sed will support:
sed -e 1d -e 5d
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

sed to conditionally delete multiple lines

Hello I'd like to remove any line in an output file that is preceded by one or more warning messages (each warning is on a separate line). Example : WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0. 822 28447 ... (4 Replies)
Discussion started by: jossojjos
4 Replies

5. Shell Programming and Scripting

Delete multiple lines in a file

The high level requirement is as follows: I have a file which has multiple line starting with pattern (which is fixed say "Hello" and i need to search for one more pattern in that line which starts with "Hello" and if the pattern matches, i need to delete lines from that line to the next line... (5 Replies)
Discussion started by: KeerthiReddy
5 Replies

6. Shell Programming and Scripting

Delete multiple lines from a file

Hi, I'm trying to delete some entry's, the source is a file1, from file2 what I have until now is this file1 : 68255706,234200801053269,447916926187,8944200006353029289F 73495477,234200101579319,447861769299,8944200006852033303F file2: 353851164675 NEW : 272050001241889 -ok ... (10 Replies)
Discussion started by: BlueRay86
10 Replies

7. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

8. Shell Programming and Scripting

using sed command to delete a string spanning multiple lines

file1 contains the following data sssssssssss firstline secondline pppppppppp ssssssssss Using sed comamnd i am trying to delete firtsline secondline. so, output should be sssssssssss pppppppppp ssssssssss I tried in the following the way, but it is not working. sed ... (9 Replies)
Discussion started by: radha.kalivar
9 Replies

9. UNIX for Dummies Questions & Answers

Delete multiple lines containting a variable string using SED.

Good morning, Novice scripter in Unix here, and I've run into and sed task I can't quite wrap my head around. I'm pulling my hair out fast enough as it is and thought I would go to the knowledge bank. I have a sorted file that I'm trying to trim down by deleting any line whose first few... (2 Replies)
Discussion started by: selkirk
2 Replies

10. Shell Programming and Scripting

Need to delete multiple lines in a file.

Hi, I'm new to this forum, and searched through the previous posts, but didn't see anything close enough to what i'm looking for. I have a radius file like this: testone Password = "11111" Service-Type = "Framed-User", Session-Timeout =... (6 Replies)
Discussion started by: kangdom
6 Replies
Login or Register to Ask a Question