deleting text between two stings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting text between two stings
# 1  
Old 12-28-2009
deleting text between two stings

Code:
example.txt
----------
this is a line i want to keep
this is another line I wish to keep
I wish to delete from here ON until I see
four new lines from here and then
I wish to keep the rest.
These are some special charcters {)#@ which 
have to be deleted too
This is a one more new line here. delete the text from 
here. The end.

I wish to delete all the text from the word "ON" in the 3rd line
until the word "deleted" using sed.
I appreciate your solutions.

I have tried this so far , It works but there it removes the complete
lines 4 till 7
Code:
sed -e /ON/deleted//d example.txt > out.txt


Last edited by jville; 12-28-2009 at 02:10 PM..
# 2  
Old 12-28-2009
Tools a strange approach, but appears to work

Code:
C:\cygwin\tmp>cat example.txt
this is a line i want to keep
this is another line I wish to keep
I wish to delete from here ON until I see
four new lines from here and then
I wish to keep the rest.
These are some special charcters {)#@ which
have to be deleted too
This is a one more new line here. delete the text from
here. The end.

C:\cygwin\tmp>sed s/ON/~ON/ example.txt | sed s/deleted/deleted~/ | tr \n "^" | cut -d"~" -f1,3 | tr "^" \n | tr -d "~"
this is a line i want to keep
this is another line I wish to keep
I wish to delete from here  too
This is a one more new line here. delete the text from
here. The end.

And it shows 'creative' use of sed, cut, tr commands!
# 3  
Old 12-28-2009
Hi, joeyg.

Quote:
Originally Posted by joeyg
Code:
C:\cygwin\tmp>sed s/ON/~ON/ example.txt | sed s/deleted/deleted~/ | tr \n "^" | cut -d"~" -f1,3 | tr "^" \n | tr -d "~"

And it shows 'creative' use of sed, cut, tr commands!
This solution works with this specific data sample, but it will fail badly if the data contains '~' or '^' characters. The presence of '~' characters could cause the cut command to slice things up incorrectly, and any '^' in the original text will be lost and new newlines inserted in their place.

Perhaps you realized this, but I just wanted to point it out in case others did not.

Take care,
alister
# 4  
Old 12-28-2009
Tools Yes, I did realize the use of those two characters

My bad for not pointing that out. I specifically chose a couple of characters that are little used, and not present in the example.
# 5  
Old 12-28-2009
Code:
sed -e '
 /ON/,/deleted/ {
    /ON/s/ON.*$//p
    /deleted/s/^.*deleted//p
    d
 }' example.txt > out.txt

This solution is greedy. If the word 'deleted' occurs more than once on the line that ends the range, everything until the final 'deleted' will be removed.

Last edited by alister; 12-28-2009 at 03:40 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to generate adler32 stings that convert into hex stings in python 2.7?

I want to generate adler32 stings that converts into hex stings in python 2.7 (1 Reply)
Discussion started by: bigvito19
1 Replies

2. Shell Programming and Scripting

Script to replace stings in multiple text files

Good Evening Folks - Happy Friday! I have a need to replace a certain string in all .csv files from "0.00" to "#Missing" in my /app/hyp_app/files directory. Does anyone have a script they use regularly that's rather quick in performance? My files are rather large so I'm looking for a... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

3. UNIX for Dummies Questions & Answers

Deleting Block of Text from a File

Hi I am looking for the way to delete the block of data for example original file line1 line2 line3 line4 line5 input file line2 line3 original file should contain line1 line4 line5 (3 Replies)
Discussion started by: rakeshkumar
3 Replies

4. UNIX for Dummies Questions & Answers

deleting text with sed

Hi There! I've got a tab delimited text file (output from a software) to which I would like to delete specific strings from one of the columns. I have tried several sed codes, but they do not seem to work for me. I can manage to delete a specific word, but this is of no use, as I what I want to... (3 Replies)
Discussion started by: alfredman
3 Replies

5. Shell Programming and Scripting

help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has username@email.com:John:149.0.3.4:1 username1@email.com:Harry:149.0.3.4:1 username1@email.net:Alex:149.0.3.4:1 username1@email.edu:Nemo:149.0.3.4:1 The program i written ... (3 Replies)
Discussion started by: ichar
3 Replies

6. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

7. Shell Programming and Scripting

deleting text between ; and :

An ldif export needs some text removed from every line with a ; and : Only the text (including the ;) from the ; to the : (but not the :) needs to be removed. thoughts? here's an example of one record: dn: cn=me,ou=groups,dc=me,dc=com nsUniqueId: 1255060a-46e511dd-803ce5f5-385faa0b... (1 Reply)
Discussion started by: rfransix
1 Replies

8. Shell Programming and Scripting

Deleting text block in file

Need to delete a text block inside a file, that is marked with a start and an end pattern. Eg do not delete not delete <tag1> delete everything here here and here and here... <tag2> do not delete do not delete.... Believe sed is able to do this job but don't get it working. ... (1 Reply)
Discussion started by: andre123
1 Replies

9. UNIX for Advanced & Expert Users

Please Help. Need Help searching for multiple stings in a file and removing them.

Please help. Here is my problem. I have 9000 lines in file a and 500,000 lines in file b. For each line in file a I need to search file b and remove that line. I am currently using the grep -v command and loading the output into a new file. However, because of the size of file b this takes an... (2 Replies)
Discussion started by: mjs3221
2 Replies

10. Shell Programming and Scripting

Deleting text from a file

Hi, In my korn shell script, I want to delete some particular text from a certain file...How can this be done? Is the below right? ed $NAMES << EOF echo "" > /dev/null echo "${x} = " > /dev/null echo "name = " > /dev/null echo "adress = " > /dev/null w q EOF (1 Reply)
Discussion started by: n8575
1 Replies
Login or Register to Ask a Question