SED - confused by the s command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - confused by the s command.
# 1  
Old 12-07-2010
SED - confused by the s command.

Code:
echo "abc 123" | sed 's/[0-9]*/& &/g'
output:
 a b c  123 123

Why there are spaces between the "abc" letters?

Code:
echo "abc 123" | sed 's/[0-9]*/&&/'
output:
abc 123

Why the regex in the above script does not match anything? I thought [0-9]* should match 123 in any case.

---------- Post updated at 08:25 AM ---------- Previous update was at 08:17 AM ----------

ok, for the second question, I figured it out.
it is because sed tries to match the first character and it finds it does not match the input string and it stops further attempt because there's no "g" at the end of the pattern(no global search is needed).
# 2  
Old 12-07-2010
[0-9]* means 0 or more occurrence.

to match 123 u have to write [0-9]+ or [0-9][0-9]* means 1 or more occurrence.
This User Gave Thanks to For This Post:
R0H0N
# 3  
Old 12-07-2010
As ROHON said:

What happen when check ing "a" :
does in match 0 or more occurrence of [0-9] ?
answer : yes, it matches 0 occurrence of [0-9] ...

you should go for the [0-9][0-9]* instead
...or, if your sed version can support extended character set for the regular expression, you can then use [0-9]+
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 12-07-2010
Thank you R0H0N and ctsgnb,
My understanding was wrong.
With your clarification, I understand it now, Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confused by the sed command

Hi Gurus, I am a little bit confused by the sed command. my file is below cat sample aaa bbb ccc ddd eee fff ggg hhh iii below is command and output. (6 Replies)
Discussion started by: ken6503
6 Replies

2. Shell Programming and Scripting

Confused by command substitution and quoting

How do I assign output of a command to a variable without expanding it? I have tried every combination of quoting I can think of including literal quotes in the command and user agent variable and still can't get it to work (or it breaks Lynx because Lynx tolorates spaces in the useragent but... (2 Replies)
Discussion started by: Michael Stora
2 Replies

3. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. UNIX for Dummies Questions & Answers

Confused with sed...

Yes I know this is a bad use of cat, but just ignore it please :P My problem is with sed. File: "newFile" contains: day Command I run: cat newFile | sed 's/day/night' This returns: sed: -e expression #1, char 11: unterminated `s' command Why??? (1 Reply)
Discussion started by: maximus73
1 Replies

6. UNIX for Dummies Questions & Answers

Confused over results using "tee" in piped command

First post here, so hopefully all guidelines are followed, and thanks in advance for any replies. I'm working on a shell script(BASH) that processes a csv file and performs various tasks with the data. All is well, except I want to use 'tee' to send output from 'wc' to a file as well as pipe it... (4 Replies)
Discussion started by: jazzmusic
4 Replies

7. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

8. Solaris

Confused with echo $SHELL Command....

Hi.. Everyone... Kindly consider following : login as: root Using keyboard-interactive authentication. Password: Last login: Mon Nov 3 19:30:50 2008 from xxxxxxxxxxx Sun Microsystems Inc. SunOS 5.10 Generic January 2005 You have new mail. Sourcing //.profile-EIS..... # # ... (3 Replies)
Discussion started by: Reboot
3 Replies

9. Shell Programming and Scripting

sed very confused

Hello experts, I have this complicated code that output value in between pattern and add "a string" to the front of the output. The problems I have many pattern that are the same. so I want to know how to get the value between 1st pattern, 2nd pattern etc. Any suggestions? sed -n... (14 Replies)
Discussion started by: minifish
14 Replies
Login or Register to Ask a Question