Register
It is currently Thu Jul 24, 2014 6:19 am

deleting specific text lines


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sat Sep 08, 2007 3:52 pm   

Joined: Sat Sep 08, 2007 3:31 pm
Posts: 1
Hello everyone. I need a bash script that can delete specific lines from text file.
For example i have a text file that contains the following lines:
Quote:
// X starts here
text
text
// X ends here

// Y starts here
text
text
// Y ends here

and i want this script to delete the lines from "X starts here" to "X ends here" by script.sh delete X
10x in advance


Top
 Profile  
 PostPosted: Mon Sep 10, 2007 9:34 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i kind of understand what you are wanting... but i'm not real sure on the format for the text file you are trying to parse... a little more descriptive information would be appreciated :wink:


Top
 Profile  
 PostPosted: Mon Sep 10, 2007 8:15 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
martien wrote:
Hello everyone. I need a bash script that can delete specific lines from text file.
For example i have a text file that contains the following lines:
Quote:
// X starts here
text
text
// X ends here

// Y starts here
text
text
// Y ends here

and i want this script to delete the lines from "X starts here" to "X ends here" by script.sh delete X
10x in advance


Well.... to be honest this sounds alot like homework.......

But.... I can give you hints on how I would do it......
If your wanting to delete a section of text by LINE number.......... then you need the starting line number to delete and the ending line number to delete... this would be in the form of ./script.sh 1 10 giving you two inputs for the script $1 and $2.

Using the first number.... create a new variable... "topoffile" with topoffile=`$1 - 1`
Now........ you can put the top part of your file into another file........... head $topoffile > NEWFILENAME

Using the second input number you can get the bottom part of your file......... bottomoffile=`$2 +1`
Now you can put the bottom part into your newfile...... tail $bottomoffile >> NEWFILENAME

if you wanted to overwrite your existing file with the new file.......... mv NEWFILENAME oldfilename

I didn't write the script for you.......... but you can play with what I gave you and probably not have too hard a time from this.........

jbsnake might have a better solution up his sleeve.......... you never know...... ;)


Top
 Profile WWW  
 PostPosted: Thu Oct 04, 2007 5:29 am   

Joined: Wed Jul 25, 2007 11:45 am
Posts: 11
For this specific issue:

Code:
$ cat bla
// X starts here
text
text
// X ends here

// Y starts here
text
text
// Y ends here

$ egrep -v "(starts|ends) here" bla
text
text

text
text


Top
 Profile  
 PostPosted: Tue Oct 09, 2007 4:53 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Sed is the bestest command ever! Check it out. I added some lines to your example just to demonstrate how neatly sed snips out the lines that you want to remove:

Code:
wendtj@HOUWENDTJ03>$ cat tmp.txt
random
text
goes
here
// X starts here
text
text
// X ends here
i really like rice
it is my favorite grain
beer is also good
// Y starts here
text
text
// Y ends here
monkeys
took
my brain
for
experimentation
in
a laboratory

wendtj@HOUWENDTJ03>$ sed '/starts.here/,/ends.here/d' tmp.txt
random
text
goes
here
i really like rice
it is my favorite grain
beer is also good
monkeys
took
my brain
for
experimentation
in
a laboratory


Is that what you were looking for?

-jeo


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP