Removing string between two particular strings in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing string between two particular strings in a line
# 8  
Old 01-07-2012
Thanks a ton!!! great!

Now I have one more query to you guys regarding how to use variables in sed cmmand. PLease find beow situation:

I have a input file as below:
Code:
this is line 1
line number 2
"now" it is three
line 4
note* this five

output should look like:
Code:
line 1
line 2
line three
line 4
line five

To do this I created rule_file like below
Code:
this is|
number|
"now" it is|line
note* this|line

And following is the script:
Code
Code:
rule_file=$1
i=0
j=1
cp ./infile ./ofile_${i}
while read line; do
curr_text="`echo ${line} | awk -F\"|\" '{print $1}'`"
new_text="`echo ${line} | awk -F\"|\" '{print $2}'`"
sed "s/${curr_text}/${new_text}/g" ./ofile_${i} > ./ofile_${j}
i=${j}
j=`expr $j + 1`
done < ${rule_file}

This script is giving me following output:
Code:
line 1
line 2
"now" it is three
line 4
note* this five

It is not able to handle " (double quotes) and * (asterisk). Can anybody please help me? Thanks!

Last edited by zxmaus; 01-07-2012 at 07:04 AM.. Reason: added more tags
# 9  
Old 01-07-2012
Can't you just remove all the " and * from the beginnning
adding the s/["*]//g substitution to the previous sed statement ?

Code:
sed 's/[([][^])]*[])]//g;s/["*]//g' yourfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching some string in a line and removing that

I have one output file. Node: hstg1so Date: 2013/07/16 17:51:24 GMT Totals: 10608 6871 0 2208 1529 0 0 64% 0% ( 0 ) Node: hstg2so Date: 2013/07/16 17:51:25 GMT Totals: ... (3 Replies)
Discussion started by: Raza Ali
3 Replies

2. Shell Programming and Scripting

Removing command line arguments from string list

I am passing a list of strings $list and want to remove all entries with --shift=number, --sort=number/number/..., --group=number/number/... Also are removed whether upper or lower case letters are used For example the following will all be deleted from the list --shift=12 --shift=2324... (7 Replies)
Discussion started by: kristinu
7 Replies

3. Shell Programming and Scripting

script in perl for removing strings between a file

I have file that looks like: ATOM 2517 O VAL 160 8.337 12.679 -2.487 ATOM 2518 OXT VAL 160 7.646 12.461 -0.386 TER ATOM 2519 N VAL 161 -14.431 5.789 -25.371 ATOM 2520 H1 VAL 161 -15.336 5.698 -25.811 ATOM 2521 H2 VAL 161 -13.416 10.529 17.708 ATOM 2522 H3 VAL 161 -14.363 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

4. Shell Programming and Scripting

Removing a line IF the next line contains string

So, I've been working on a project which takes layer 7 metadata from pcap dumps and archives it. However, there is a lot of dataless information that I don't want in my output. I know of ways to produce the output I want from the input file below, but I want a method of doing this, regardless of... (2 Replies)
Discussion started by: eh3civic
2 Replies

5. SuSE

finding and removing block of identical strings

i have a problem in finding block of identical strings...i solved the problem in finding consecutive identical words and now i want to expand the code in order to find and remove consecutive identical block of strings... for example the awk code removing consecutive identical word is:... (2 Replies)
Discussion started by: cocostaec
2 Replies

6. Shell Programming and Scripting

finding and removing block of identical strings

i have a problem in finding block of identical strings...i solved the problem in finding consecutive identical words and now i want to expand the code in order to find and remove consecutive identical block of strings... for example the awk code removing consecutive identical word is:... (2 Replies)
Discussion started by: cocostaec
2 Replies

7. Shell Programming and Scripting

grep a string and print strings on that line

i have a file like below ABS 12234 A C 12G CFY 23865 A C 34D i want to grep "A C" then print ABS 12G 12234 CFY 34D 23865 Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

8. Shell Programming and Scripting

Perl removing strings from a variable value

Dear all, I have a variable called $abc, which the value is something like below, *** *********** : ***** where * can be anything. I need to remove all but the final characters until last whitespace. example grd groupstudy : tutor6/7 becomes tutor6/7 something like if... (2 Replies)
Discussion started by: tententen
2 Replies

9. Shell Programming and Scripting

Removing text between two static strings

Hi everyone, I need to replace the text between two strings (html tags) and I'm having trouble figuring out how to do so. I can display the text with sed but I'm not having any luck deleting the text between the two strings. My file looks like this: <oths>test</oths><div class="text">1928... (2 Replies)
Discussion started by: cg2
2 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question