Replace consecutive occurrence of string in same line


 
Thread Tools Search this Thread
Operating Systems AIX Replace consecutive occurrence of string in same line
# 1  
Old 03-28-2017
Replace consecutive occurrence of string in same line

Hi All,

I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output.

Input:
---------
HTML Code:
123.5|ABC|.|.|.
234.4|DEF|.|.|.|.|.|
Output:
---------
HTML Code:
123.5|ABC|||.
234.4|DEF|||||
so basically "|.|" need to be replaced with "||"


I tried with sed but its replacing only once.
sed 's/|\.|/||/g' . below is the output from this sed command.

HTML Code:
123.5|ABC||.|.
234.4|DEF||.||.||
Please any one provide the correct command.
# 2  
Old 03-28-2017
Code:
$ sed "s/\.|/|/g" file
123.5|ABC|||.
234.4|DEF||||||

This doesn't really satisfy the "requirement to replace consecutive occurence of same string", it only removes full-stops.
This User Gave Thanks to Scott For This Post:
# 3  
Old 03-28-2017
Hi Scott,
thank you for checking this issue. your solution is working for most of the cases of my input file but its giving issue if the data as below.

Input:
HTML Code:
123.5|ABC.|.|.|.
234.4|DEF|.|.|.|.|.|
Desired output :
HTML Code:
123.5|ABC.|||.
234.4|DEF||||||
But your solution is giving the output as below.

HTML Code:
123.5|ABC|||.
234.4|DEF||||||
So basically if the string ABC has included '.' then its not working as expected. So I thing we need to look for the entire string "|.|" instead of ".|"

Thank you.
# 4  
Old 03-29-2017
With the g modifier, what matched once is out of scope for another match.
But a loop can do it
Code:
sed '
:L
s/|\.|/||/
tL
'

Another solution is perl: by using a look-ahead an RE substitution with g modifier will do it.

---------- Post updated at 03:10 ---------- Previous update was at 00:58 ----------

For completeness, here it is:
Code:
perl -pe 's/\|\.(?=\|)/|/g'

The (?= ) is the look-ahead; hard to remember, I always consult this tutorial.
Because the look-ahead is not part of the match, it must be not restored in the substitution.
Perl(version >= 5) uses an extended regular expression: | means "or", must be \escaped.

Last edited by MadeInGermany; 03-29-2017 at 05:18 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 03-29-2017
Another solution is to just run your global substitution twice:
Code:
sed -e 's/|\.|/||/g' -e 's/|\.|/||/g'

which, with your latest sample input:
Code:
123.5|ABC.|.|.|.
234.4|DEF|.|.|.|.|.|

produces the output:
Code:
123.5|ABC.|||.
234.4|DEF||||||

These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-29-2017
Running the sed twice will also fixing the issue but the there are chances that the string will repeat more than twice also and hard to identify how many times the string will repeat in the file.
Hence used the perl solution which is working in all conditions.
Thank you so much all of you for the solution.
# 7  
Old 03-29-2017
Quote:
Originally Posted by ureddy
Running the sed twice will also fixing the issue but the there are chances that the string will repeat more than twice also and hard to identify how many times the string will repeat in the file.
Hence used the perl solution which is working in all conditions.
Thank you so much all of you for the solution.
I didn't say to run sed twice; I said to run sed once using the global substitution twice. Running that global substitute twice will take care of ALL occurrences of the pattern you said you wanted to change. You might remember that the 2nd sample input you provided:
Code:
234.4|DEF|.|.|.|.|.|

contained 5 occurrences of the pattern you wanted to modify and that sed command produced exactly the output you said you wanted:
Code:
234.4|DEF||||||

removing all 5 occurrences of periods between vertical bars; not just two of them.
This User Gave Thanks to Don Cragun 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

Replace first occurrence of a string in while loop

####Solved#### Hello, My aim is to replace searched string with incremented value under ubuntu 16.04. Example: aasasasas 9030 31wwo weopwoep weerasas 9030 ew31wo ieopwoep bbqqqsas 9030 ew3swo ieeopwoep ccsaqpas 9030 ewiro o2opwoep Expected: aasasasas 9030 31wwo weopwoep weerasas 9031... (2 Replies)
Discussion started by: baris35
2 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

4. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

5. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. UNIX for Dummies Questions & Answers

Search and replace to first occurrence of string

Hi all, I have a very large; delimited file. In vi I would like to replace: CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A with: CSACT_DY;AVG_UEACT1;Average... (7 Replies)
Discussion started by: gilmord
7 Replies
Login or Register to Ask a Question