Problem with Sed when repeating characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with Sed when repeating characters
# 1  
Old 03-26-2010
Problem with Sed when repeating characters

Hi all,

I'm learning sed (and regular expressions) - My first little program is to replace 3 numbers in a row with 'XXX'

This is what I am trying:
Code:
echo '511' | sed 's/[0-9]{3}/XXX/'

Here is the output:
Code:
defunct-macbook-pro:~ defunct$ echo '511' | sed 's/[0-9]{3}/XXX/'
511

For some reason, it doesnt replace. Maybe I am screwing up something, maybe its the fact I am using a mac.

I am running OSX Leopard 10.6.

Any thoughts?

Thank you very much!
# 2  
Old 03-26-2010
Quote:
Originally Posted by Defunct
Maybe I am screwing up something, maybe its the fact I am using a mac.
Sorry for being so blunt, but it is not the Mac which is to blame:

The regexp qualifier for "several of the expressions before" has to be escaped:

Code:
s/[0-9]\{3\}/XXX/

This searches for 3 consecutive digits (0-9) and replaces it with "XXX".

I hope this helps.

bakunin
# 3  
Old 03-26-2010
Code:
echo '511' | sed -r 's/[0-9]{3}/XXX/'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Indexing each repeating pattern of rows in a column using awk/sed

Hello All, I have data like this in a column. 0 1 2 3 0 3 4 5 6 0 1 2 3 etc. where 0 identifies the start of a pattern in my data. So I need the output like below using either awk/sed. 0 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

2. Shell Programming and Scripting

awk and or sed command to sum the value in repeating tags in a XML

I have a XML in which <Amt Ccy="EUR">3.1</Amt> tag repeats. This is under another tag <Main>. I need to sum all the values of <Amt Ccy=""> (Ccy may vary) coming under <Main> using awk and or sed command. can some help? Sample looks like below <root> <Main> ... (6 Replies)
Discussion started by: bk_12345
6 Replies

3. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

4. UNIX for Dummies Questions & Answers

Using SED to delete between two blocks.....and then repeating.

Hi All I'm still on my slow and painful self teach learning experience with SED. My latest issue is getting my head around how best to do the following. I have a file that's created using iwlist that I want to chop up into paragraphs then only keep the ones I see as potential threats. I... (3 Replies)
Discussion started by: Bashingaway
3 Replies

5. Shell Programming and Scripting

Sed Replace repeating pattern

Hi, I have an sqlplus output file using the character ';' as a delimiter and I would like to replace the fields without datas (i.e delimited by ';;') by ';0;' Example: my sqlplus output: 11;22;33;44;;;77;; What I would like to have: 11;22;33;44;0;0;77;0; Thanks in advance for your... (2 Replies)
Discussion started by: popesk
2 Replies

6. Shell Programming and Scripting

Sed Replace a repeating character

I have a text file and every line ends in |^ |^^ |^^^ |^^^^ I need to use sed to make all lines end it |^ regardless of the amount of carrots. The code i was using is: cat FILE | sed 's/\^\^\^/\^/g' But then they threw that curveball at me. Also is there a way to... (2 Replies)
Discussion started by: insania
2 Replies

7. Shell Programming and Scripting

Repeating groups problem

Hi I am trying to locate all strings seperated by the string -CR-, the string will have an unknown number of these -CR- strings, I've used the regex: ^(?:(.*?)-CR-)+$ As shown in the test code: my ($myString)="This is the first line-CR-second line-CR-third line-CR-fourth... (3 Replies)
Discussion started by: joncoop
3 Replies

8. Shell Programming and Scripting

Value repeating problem in columns

Hi, I have a file like this 0817 0201364 1 866 . . . . . . . 574 . 100.0 100.0 5529737 1 TV 0817 0201364 2 1440 . . . . . . . . . . . 5529737 1 TV 0817 0201364 6 1323 . . . . ... (2 Replies)
Discussion started by: Sandeep_Malik
2 Replies

9. UNIX for Dummies Questions & Answers

assitance with sed (repeating patterns)

hi, I need to write a command to look into a text file, find lines that contain patterns of three or more characters that repeat once, and put perenthesizes around them. so for example, the line "123test123" would be changed to "(123)test(123)" and "abcdeabcde" to "(abcde)(abcde)". any hint is... (7 Replies)
Discussion started by: metalwarrior
7 Replies

10. UNIX for Advanced & Expert Users

need assistance: sed and repeating patterns

hi, I need to write a command with sed to find all the lines in a file that contain patterns of three or more characters that repeat once and put them inside perenthezes. I cannot tell sed what pattern to look for. it should find repeated patterns automatically. example:... (1 Reply)
Discussion started by: metalwarrior
1 Replies
Login or Register to Ask a Question