Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 07-24-2003
Registered User
 

Join Date: Dec 2001
Posts: 5
Unhappy 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
Sponsored Links
  #2 (permalink)  
Old 07-24-2003
criglerj's Avatar
Registered User
 

Join Date: May 2002
Location: Atlanta
Posts: 129
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 (permalink)  
Old 07-25-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,150
Modern versions of sed support:
sed '1d;5d'

Even the very first version of sed will support:
sed -e 1d -e 5d
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regex to delete multiple blank lines in a file? fedora Shell Programming and Scripting 6 10-11-2007 05:36 PM
using sed command to delete a string spanning multiple lines radha.kalivar Shell Programming and Scripting 9 07-25-2007 11:26 AM
Delete multiple lines containting a variable string using SED. selkirk UNIX for Dummies Questions & Answers 2 04-27-2007 08:08 PM
Need to delete multiple lines in a file. kangdom Shell Programming and Scripting 6 10-16-2006 12:02 PM
delete multiple empty lines whatisthis Shell Programming and Scripting 3 11-09-2005 05:42 PM



All times are GMT -4. The time now is 11:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0