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