Visit Our UNIX and Linux User Community


sed 'iteration character' question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed 'iteration character' question
# 1  
Old 10-13-2009
sed 'iteration character' question

I'm processing ( trying to anyway ) a file of pipe delimited database output.

Sample record:
Code:
365026 | ocn424525329 | With his lightening-quick hands and feet

The second field in the above data is "ocn424525329". It should be
"424525329" with no trailing or leading spaces. This may be better approached with awk but while I try that I thought I'd ask something that's been bothering me about sed's iteration metacharacter: {n} or {n,m}.

For example, suppose I want to replace this field with the word 'hello' ...

Why can I not match this with:
Code:
sed 's/ocn[0-9]{9}/hello/g' tmp

???

No matter what I try the {n} meta sequence does nothing.

Bub

---------- Post updated at 07:02 PM ---------- Previous update was at 06:55 PM ----------

The following matches:

Code:
sed 's/oc[nm][0-9]\{8,9\} /hello/g' tmp

So I have to escape the braces. When un-escaped, I assume that they are
taken literally?

thanks ~

Bub
# 2  
Old 10-14-2009
Quote:
Originally Posted by Bubnoff
[...]The following matches:

Code:
sed 's/oc[nm][0-9]\{8,9\} /hello/g' tmp

So I have to escape the braces. When un-escaped, I assume that they are
taken literally?
[...]
Yes, you are right. sed uses POSIX regular expressions, which are almost, but not completely, like Perl regular expressions. Character classes work almost the same, but matching groups and repetition require their braces to be escaped.
# 3  
Old 10-14-2009
Thanks ~

Handy tool once you get the rules down!

Bub

Previous Thread | Next Thread
Test Your Knowledge in Computers #224
Difficulty: Easy
In September 2019, according to NetMarketShare, Windows had just over 87% of the global desktop market, followed by Linux at close to 10%, and macOS in third place at around 2%.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to remove first and last character using sed

Hi I have file in below format. How i can remove the first and lost comma from this below file ,001E:001F,,,02EE,0FED:0FEF, I need output has below 001E:001F,,,02EE,0FED:0FEF (6 Replies)
Discussion started by: ranjancom2000
6 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

4. Shell Programming and Scripting

sed help with character removal

Hello I've got a string of text with a number in pence, e.g. 0.52p, I need to remove the 'p' so that it just reads 0.52 without of course removing all the other 'p' characters. Many thanks (1 Reply)
Discussion started by: mrpugster
1 Replies

5. Programming

Question how to replace last character string

Hi, Could someone help me how to replace last character string. For example $>export T1=abcde $>export T2=xyz my question is how to get result abcdxyz? Many Thanks (2 Replies)
Discussion started by: nicklau81
2 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

7. UNIX for Dummies Questions & Answers

SED: Can't Repeat Search Character in SED Output

I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml: <MY_BIG_TAG>This_is_a_test</MY_BIG_TAG> It should look like... (8 Replies)
Discussion started by: rhetoric101
8 Replies

8. UNIX for Advanced & Expert Users

Unix wildcard character question

1. Is . wildcard? , the documented wildcard are "*", "?", and "" . seems mean everything, the follwing cmd will copy everything cp -r /tmp/test1/. /tmp/test2/ However it doesn't work for rm, why? $ ls -a . .. .a .aa aa t2 $ rm -rf . $ ls -a . .. .a .aa ... (3 Replies)
Discussion started by: honglus
3 Replies

9. Shell Programming and Scripting

Escape character - sed

Hi All, How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character? Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors :( Example of etc/passwd file: blah:x:1055:600:blah... (6 Replies)
Discussion started by: c00kie88
6 Replies

10. UNIX for Dummies Questions & Answers

possible to escape the \ character in sed?

is it possible to escape the \ character in sed? right now I'm trying to replace all occurances of \ with \\ sed \"s|test|test_replacement|g\" file1 > output; #this works fine sed \"s|\\|\\\|g\" file1 > output; #this generates the following error: sed: -e expression #1, char 17:... (1 Reply)
Discussion started by: gammaman
1 Replies

Featured Tech Videos