Removing last occurance of string in text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing last occurance of string in text
# 1  
Old 08-29-2011
Removing last occurance of string in text

I have text file as follows and would like to remove the last occurance of "UNION ALL" string and replace @@ with single quote (').

Input text in file is
Code:
with temp as ( 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
) 
select xxxx, yyyyyy from temp with ur  ;

Output expected is
Code:
with temp as ( 
( select -----------  where OPERATION = 'B' and OBJECTTYPE = 'P' and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = 'B' and OBJECTTYPE = 'P' and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = 'B' and OBJECTTYPE = 'P' and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = 'B' and OBJECTTYPE = 'P' and  start_time desc ) 
) 
select xxxx, yyyyyy from temp with ur  ;

Appreciate your help .

Last edited by Vaddadi; 08-29-2011 at 04:56 PM.. Reason: Code tags
# 2  
Old 08-29-2011
Try:
Code:
perl -p0e 's/@@/\x27/g;s/UNION ALL\s+\n\)/\n)/s' file

# 3  
Old 08-29-2011
Thanks .. anythig using sed or awk commands or any native unix commands.
# 4  
Old 08-29-2011
What do you suggest is the best indication of the last line of "UNION ALL"?
# 5  
Old 08-29-2011
Try this:
Code:
awk -v q="'" '/UNION ALL$/{
  gsub("@@",q); if(s)print s; s=$0; f=1; next}
f{
  sub(" UNION ALL$",x,s) ;print s;f=0; s=""
}
1' file

# 6  
Old 08-29-2011
Code:
ed -s file <<'EOED' >/dev/null
1,$s/@@/'/g
1
?UNION ALL?s/UNION ALL//
w
q
EOED

Regards,
Alister
# 7  
Old 08-29-2011
Am I doing something wrong , it's not removing the last occurance of "UNION ALL" . lets think about only removing the last occurance of "UNION ALL"

Code is:

Code:
#! /usr/bin/ksh
. ~/.profile

awk -v q="'" '/UNION ALL$/{
  gsub("@@",q); if(s)print s; s=$0; f=1; next}
f{
  sub(" UNION ALL$",x,s) ;print s;f=0; s=""
}
1' text1.txt    >  text_output.txt


Infile text.txt is:


Code:
with temp as ( 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL  <------- remove this 
) 
select xxxx, yyyyyy from temp with ur  ;


Output file text_output.txt looks like:


Code:
with temp as ( 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
( select -----------  where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and  start_time desc ) UNION ALL 
) 
select xxxx, yyyyyy from temp with ur


Last edited by radoulov; 08-29-2011 at 05:17 PM.. Reason: Code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert text after the first occurance of searched string entry in a file

My server xml file has huge data part of which i'm sharing below. I wish to add the below text held by variable "addthisline" after the closing braces i.e --> once the first </Connector> tag is found. addthisline="I need to be inserted after the comments" Thus my searchstring is... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

3. Windows & DOS: Issues & Discussions

Removing anything from text file except specific string

So, I have a text file that looks like this: 0,0: (168,168,176) #A8A8B0 srgb(168,168,176) 1,0: (168,168,176) #A8A8B0 srgb(168,168,176) 2,0: (166,166,174) #A6A6AE srgb(166,166,174) 3,0: (166,166,174) #A6A6AE srgb(166,166,174) 4,0: (168,168,176) #A8A8B0 srgb(168,168,176) 5,0:... (0 Replies)
Discussion started by: pasc
0 Replies

4. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

5. Shell Programming and Scripting

extract till occurance of a string

hi , i have an xml that comes in a single, the entire xml file is read as a single line when i open in edit plus or unix. i need to amend the contents of this xml file. below is the extract from the file <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"... (5 Replies)
Discussion started by: sais
5 Replies

6. UNIX for Dummies Questions & Answers

Removing a string of text from a file - help please

Hey Folks, I have a file that contains data that I am working with, sometimes this file has a very long string of text that messes with an awk command in a script i am trying to build. I would like to cut this string of text out of a file and then redirect everything except that string to a new... (5 Replies)
Discussion started by: deepslp
5 Replies

7. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

8. UNIX for Dummies Questions & Answers

String search - Command to find second occurance

Hi, I am new to Unix world. Is there any command which can directly return the second occurance of a particular string in a file? Basically, I want to read the contents of the file from the second occurance of a particualr string. Can be implemented using a loop, but am just wondering if there... (5 Replies)
Discussion started by: saurabhsinha23
5 Replies

9. UNIX for Dummies Questions & Answers

count string occurance in a file hourly

Hi, I file that has all the status for one day (24hours). Now what I want to do is to count the occurence of a string in its output hourly like for example count occurance of successful or asynchronous clear destinon for every hour and redirect it to file. Please see sample file below. Please... (2 Replies)
Discussion started by: ayhanne
2 Replies

10. Shell Programming and Scripting

Removing the last occurance of string

I have a small query. I have a file containing the following lines abcd<12></12>fdfgdf<12>sdfgfg<12> sdfsdf<12></12>ytunfg<12> hggfhf<12>rtysb<12>zdfgdfg<12> Now I wish to delete ONLY the last occurance of string <12> from every lines of code. That mease my final output will be like this:... (7 Replies)
Discussion started by: dkhanna01
7 Replies
Login or Register to Ask a Question