sed remove multiple set of lines in one command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed remove multiple set of lines in one command
# 1  
Old 01-13-2011
sed remove multiple set of lines in one command

is there a way with sed to removed more than one set of lines in one line?

so i mean
Code:
sed ${firstElem},${lastIndex}d web.xml > web1.xml

this will delete lines between ${firstElem},${lastIndex}

i want in the same line to do somethinkg like this (doesn't work so far)
Code:
sed ${firstElem},${lastIndex}d ; ${secondElem},${lastIndex1}d web.xml > web1.xml

at the same time delete lines between ${firstElem},${lastIndex} and ${secondElem},${lastIndex1}d

if i don't do this in one line.. indexes for the second set change.. and i need to grep for them again.. so it would be really good if i could delete the two sets simulatiously..

thanks a lot

Last edited by Scott; 01-13-2011 at 01:09 PM.. Reason: Code tags, please...
# 2  
Old 01-13-2011
Yes you can, but you need to use double quotes
Code:
sed "${firstElem},${lastIndex}d ; ${secondElem},${lastIndex1}d" web.xml > web1.xml

I take it the variables contain numbers or patterns in between slashes?
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-13-2011
Quote:
Originally Posted by Scrutinizer
Yes you can, but you need to use double quotes
Code:
sed "${firstElem},${lastIndex}d ; ${secondElem},${lastIndex1}d" web.xml > web1.xml

I take it the variables contain numbers or patterns in between slashes?

correct that variables contain line numbers.. so

like delete
10 to 13 , and 45 to 47
# 4  
Old 01-13-2011
Code:
awk 'NR!~/^(1[0-3]|4[5-7])$/' file

This User Gave Thanks to ghostdog74 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

2. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 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. UNIX for Dummies Questions & Answers

Using sed command to remove multiple instances of repeating headers in one file?

Hi, I have catenated multiple output files (from a monte carlo run) into one big output file. Each individual file has it's own two line header. So when I catenate, there are multiple two line headers (of the same wording) within the big file. How do I use the sed command to search for the... (1 Reply)
Discussion started by: rebazon
1 Replies

5. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

6. Shell Programming and Scripting

What's the command to remove empty lines with sed?

3 10 20 10 100 100 10000 Output: 3 10 20 10 100 100 10000 ---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ---------- sed '/^$/d' file doesn't work. (8 Replies)
Discussion started by: cola
8 Replies

7. UNIX for Dummies Questions & Answers

sed command, make multiple lines into one

:confused:Hi, I'm relativley new at unix so am having difficulties at the most basic of areas. I am trying using sed to make multiple lines into one line. For example, i would like: Mary had a little lamb to look like this Maryhadalittlelamb so far i have tried sed... (1 Reply)
Discussion started by: cavanac2
1 Replies

8. UNIX for Dummies Questions & Answers

Sed command over multiple lines in csh??

hi i have a long sed command in a csh script that won't fit on 1 line. how do i break it up correctly over multiple lines? this doesn't seem to work in csh: sed -e s/template/$IP.$NN/ \ -e s/NRG/6/ \ -e s/inputf/$IS.$NN/ \ -e s/SHIFT/10.0/ <template.egsinp > $IP.$NN.inp i get: sed:... (1 Reply)
Discussion started by: tuathan
1 Replies

9. 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

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question