Help search and replace the last occurance of match in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help search and replace the last occurance of match in a file
# 1  
Old 02-02-2018
DB Help search and replace the last occurance of match in a file

Hi I want to replace only the last occurance of "union all" in input file with ";"
I tried with sed 's/union all/;/g' in my input file, it replaced in all lines of input file

Eg:
Code:
select column1,column2 from test1 group by 2 union all
 select column1,column2 from test2 group by 2 union all
 select column1,column2 from test3 group by 2 union all

I need to replace only the last "union all" in the above with ";".

Thanks

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-03-2018 at 04:33 AM.. Reason: Added CODE tags.
# 2  
Old 02-02-2018
One way:
Code:
awk '{ arr[NR]=$0; index($0,"union all")>0  {L=NR} }
END { gsub(/union all/, "", arr{L})
          for(i=0; i<NT; i++) 
                    {print arr[i])
        }'  infile > outfile

There are some errors in the above - thanks Rudi.
See new post below

Last edited by jim mcnamara; 02-03-2018 at 10:13 AM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-03-2018
Another way:
Code:
ed -s infile <<-EOF
	?union all?s//;/
	w outfile
	q
EOF

where infile and outfile, respective, are pathnames to your input and output files.

You can just use:
Code:
	w

instead of:
Code:
	w outfile

if you want to update your input file instead of creating a new output file.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 02-03-2018
Thanks for the reply Don cragun
I am working on centos in which ed command not found
# 5  
Old 02-03-2018
Hi antosr7,

This may be one of the methods:
Code:
grep "test3" <input_filename> | sed "s/ union all/;/"

where the test3 is the keyword to extract the line and the <filename> is where the three lines can be found.

Hope that this is helpful.

Thank you.
This User Gave Thanks to JiaHong For This Post:
# 6  
Old 02-03-2018
Quote:
Originally Posted by antosr7
Thanks for the reply Don cragun
I am working on centos in which ed command not found
Does Centos have the ex utility? If so, for the features I used in the ed script in post #3, ex will work the same way with the same options. On UNIX, Linux, and BSD systems, I usually use ed instead of ex because ex is usually larger and slower to load than ed. The ex utility is bigger because it supplies some extra features that ed doesn't provide, but they aren't used in this script.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-03-2018
Code:
# expanded lines and fixed errors
awk '{ arr[NR]=$0; 
       if (index($0,"union all")>0)  
       {L=NR} 
     }
END { 
      gsub(/union all/, "", arr[L])
      for(i=0; i<NR; i++) 
             {print arr[i] }
    }'  infile > outfile

This User Gave Thanks to jim mcnamara For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl:Regex for Search and Replace that has a flexible match

Hi, I'm trying to match the front and back of a sequence. It works when there is an exact match (obviously), but I need the regex to be more flexible. When we get strings of nucleotides sometimes their prefixes and suffixes aren't exact matches. Sometimes there will be an extra letter and... (2 Replies)
Discussion started by: jdilts
2 Replies

2. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

3. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

4. Shell Programming and Scripting

Search and replace only the first occurance

Hi, I need to search a string and replace with nothing, but only the First occurring string using sed/perl/awk (3 Replies)
Discussion started by: suraj.sheikh
3 Replies

5. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

6. Shell Programming and Scripting

awk to replace second occurance

#original file . . ~ ~ Index=2 xxx replace #dont replace 1st occurance yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=3 xxx replace (3 Replies)
Discussion started by: cjjoy
3 Replies

7. Shell Programming and Scripting

search and replace the last occurance of a match in a file

HI please let me know if there is any command to search and replace only the last occurence of a string in aline. Eg: " This cat is a cat with a tail longer of all cat." I need to replace only the last "cat" in the above line. thanks (3 Replies)
Discussion started by: harikris614
3 Replies

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

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. UNIX for Dummies Questions & Answers

replace the n'th occurance in a file

Hi All, How can i replace the n'th occurance in a file. ? I have a property file like EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;1;valeur EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;2;valeur2... (2 Replies)
Discussion started by: subin_bala
2 Replies
Login or Register to Ask a Question