LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-02-2008, 02:13 PM   #1
Rikki D
LQ Newbie
 
Registered: May 2008
Posts: 2

Rep: Reputation: 0
how do I write a script using sed to delete the first and last line in multiple files


I have multiple files of varying length that all start with dump 1 on the first line and end with OCAPI> on the last line. I would like to be able to create a script using sed to remove these two lines from all of the files. An example of a file is as follows:

dump 1
DataDDS size: ?
{ ComplexStructure01 * root
{ exp * Structure { j * Int32 value: 1 } - j
{ i * Int32 value: 2 } - i
{ g * Array dimensions: [3][3][3] value type: Int16
[1][1][-]: 0 256 512
[1][2][-]: 768 1024 1280
[1][3][-]: 1536 1792 2048
[2][1][-]: 768 1024 1280
[2][2][-]: 2304 2560 2816
[2][3][-]: 3840 4096 4352
[3][1][-]: 1536 1792 2048
[3][2][-]: 3840 4096 4352
[3][3][-]: 6144 6400 6656 } - g
{ f * Array dimensions: [2][2] value type: Int16
[1][-]: 0 256
[2][-]: 512 768 } - f
} - exp
} - ComplexStructure01
OCAPI>
 
Old 05-02-2008, 02:50 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
One method:
sed -e '/dump/d' -e '/OCAP/d' filename > newfilename

This assumes that those words are always there (and nowhere else). You can do the same thing using line numbers---eg:
sed '1d' deletes the first line.
sed '$d' deletes the last line.

Best SED tutorial here: http://www.grymoire.com/Unix/Sed.html

Last edited by pixellany; 05-02-2008 at 02:59 PM. Reason: update
 
Old 05-02-2008, 04:18 PM   #3
Rikki D
LQ Newbie
 
Registered: May 2008
Posts: 2

Original Poster
Rep: Reputation: 0
When I use sed -e'/dump 1/d'-e'/OCAPI>/d'/home/rikki/workspace/Ocapi/Tests/dods/test.21.dods in the command line I get the following error:
sed: -e expression #1, char 10: extra characters after command
 
Old 05-02-2008, 10:30 PM   #4
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
This should work

Code:
sed -e '/dump/d; /OCAPI/d' filename > newfilename
Or this

Code:
sed -e '/^dump/d' -e '/^OCAPI/d' filename > newfilename
Be sure to read pixellany warning about these words being anywhere else in your file. The
second sed code at least only takes out the lines that begin with your regrex.

Last edited by davimint; 05-02-2008 at 10:40 PM.
 
Old 05-02-2008, 11:01 PM   #5
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 64
Quote:
Originally Posted by Rikki D View Post
When I use sed -e'/dump 1/d'-e'/OCAPI>/d'/home/rikki/workspace/Ocapi/Tests/dods/test.21.dods in the command line I get the following error:
sed: -e expression #1, char 10: extra characters after command
I believe those extra characters are the next -e switch. There has to be whitespace before each switch and before the filename.

BTW, the -i switch allows sed to edit in place. For example:
Code:
sed -i '1d;$d' test.21.dods
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
How to write a script to delete files? AGazzaz Linux - General 11 12-05-2007 06:43 AM
using sed to delete line to the right khairilthegreat Linux - Newbie 5 07-28-2007 02:10 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM


All times are GMT -5. The time now is 09:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration