Regex:search/replace but not for escaped character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex:search/replace but not for escaped character
# 1  
Old 10-18-2012
Regex:search/replace but not for escaped character

Hi

Input:
Code:
-
--
---
----
aa-bb-cc
aa--bb--cc
aa---bb---cc
aa----bb----cc

Output:
Code:
.
-
-.
--
aa.bb.cc
aa-bb-cc
aa-.bb-.cc
aa--bb--cc

I need to replace a simple '-' with '.' but if I want to keep the '-' I will use '--'

I would a like a solution where regex is used as search criteria and the match will be replace with a value. A solution that can be used in language.

Thx in advance
# 2  
Old 10-18-2012
This is a three step process:
  1. Convert all occurrences of -- to something that doesn't appear in your input.
  2. Convert all remaining occurrences of - to ..
  3. Convert all of the characters you converted to in step 1 back to -.
The way you do this "in language" depends on what language you want to use.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-18-2012
Here is how Don Cragun suggested using sed

Code:
sed 's/\-\-/\%/g' | sed 's/\-/\./g' | sed 's/\%/\-/g'

# 4  
Old 10-18-2012
Great solution. Thx
# 5  
Old 10-18-2012
Quote:
Originally Posted by bipinajith
Here is how Don Cragun suggested using sed

Code:
sed 's/\-\-/\%/g' | sed 's/\-/\./g' | sed 's/\%/\-/g'

Yes; or more succinctly in sed:
Code:
sed -e 's/\-\-/\%/g' -e 's/\-/\./g' -e 's/\%/\-/g'

or even
Code:
sed 's/\-\-/\%/g;s/\-/\./g;s/\%/\-/g'

or in awk:
Code:
awk '{gsub(/--/,"%");gsub(/-/,".");gsub(/%/,"-");print}'

but there are LOTS of languages. Why don't we let the submitter tell us what language he wants.
# 6  
Old 10-18-2012
Quote:
Originally Posted by bipinajith
Code:
sed 's/\-\-/\%/g' | sed 's/\-/\./g' | sed 's/\%/\-/g'

Quote:
Originally Posted by Don Cragun
Yes; or more succinctly in sed:
Code:
sed 's/\-\-/\%/g;s/\-/\./g;s/\%/\-/g'

That's a lot of unnecessary and undefined escape sequences (which are typically treated by pretending the backslash wasn't there):
Code:
sed 's/--/%/g; s/-/./g; s/%/-/g'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 10-18-2012
Yes, absolutely. I copied from the wrong place when I created my last post. Thank you alister for pointing out my mistake.

- Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help to use regex to do search and replace. Don't know how to and can't figure out how :(

Hi, Below is an excerpt from a 20000+ lines and I want to do a search and replace of a specific string but I don't know how and I can't figure out how to. Can't find an example from Google or anywhere to do what I am wanting to do. A 2018-11-21 08:42:17 TEST_TEST 2018-11-21... (9 Replies)
Discussion started by: newbie_01
9 Replies

2. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

3. Shell Programming and Scripting

Search a character and replace it with multiple lines

This is for AIX 6.1, I've a flat file and the format is like this DECLARE some statements; BEGIN some statements; END; I've to search BEGIN and replace it with the following 4 lines BEGIN For x in 1..1 LOOP BEGIN Similarly I've to search END and replace it with the... (7 Replies)
Discussion started by: Mukul Sharma
7 Replies

4. UNIX for Dummies Questions & Answers

Search for a particular word and replace the first character

Hi Unix gurus, I've a dna sequence in a file format known as fasta format (sequence header starts with > and ignored), an example shown below: >sequence_1 CGTATTCTCCGAATACC ATACG >sequence_2 CAGATTTTCAAATACCCCC In a file like this I want to do the following three search and replace. The... (4 Replies)
Discussion started by: Fahmida
4 Replies

5. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

6. Shell Programming and Scripting

Regex - search and replace

I have file which contains data in the following format all in a single line: BDW_PUBLN_ID DECIMAL(18:0) NOT NULL PRIMARY INDEX ARGO_ACCT_DEP_PI ( OFC_ID ,CSHBX_ID ,TRXN_SEQ_NUM ,PROCG_DT ) PARTITION BY RANGE_N(PROCG_DT BETWEEN DATE '2012-03-01' AND DATE '2014-12-31' EACH INTERVAL '1' MONTH );... (4 Replies)
Discussion started by: ysvsr1
4 Replies

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

8. Emergency UNIX and Linux Support

search replace regex question

Hi, I need to run a search and replace on a large database, what I need to change is all instances of #### (eg. 1764 or 1964) to (####) (eg. (1764) or (1964)) But there might be other numbers in there such as (1764) and I do not need those changed to ((1764)) How can I... (7 Replies)
Discussion started by: lawstudent
7 Replies

9. UNIX for Dummies Questions & Answers

vi Search for text, Replace with <CR> or control character.

Greetings, Using vi, how can I change the following text: -I/myviews/nexus_7400rel/vobs/nexus/platforms/97400/include -I/myviews/nexus_7400rel/vobs/nexus/modules/i2c/7400/include -I/myviews/nexus_7400rel/vobs/nexus/modules/surface/7400/include Into this:... (4 Replies)
Discussion started by: omega949
4 Replies

10. Shell Programming and Scripting

Need to search and replace based on character count

Hi, I wanted to add a newline character after every 100 characters in a file using a awk or shell without reading each line of the file. I want to run a command on the complete file. This does based on a string but i want to add a new line after every 100 characters ir-respective of the... (3 Replies)
Discussion started by: vijaykrc
3 Replies
Login or Register to Ask a Question