LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2017, 02:34 AM   #1
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Rep: Reputation: Disabled
trying to understand a simple sed command


Hi.

I'm just going over a SED tutorial and specifically the '&' and '*' special characters.

I understand how it works:

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

123 123 abc
The above is clear but let's say i run the following, i don't understand why there is a space before the output:

Code:
echo "abc 123" | sed 's/[0-9]*/& &/'
 abc 123
what is the '*' matching in this case and why is a space being added here ?
 
Old 12-15-2017, 03:11 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
An asterisk means "zero or more occurrences of the previous character or group".

In other words, the expression 's/[0-9]*/& &/' reads "replace zero or more consecutive occurrences of the digits 0-9 with whatever was matched twice, separated by a space".

Now consider this:
Code:
~$ echo foo bar | sed 's/[0-9]*/& &/'
 foo bar
So sed looks at the string "foo bar", and what do you know, there are exactly zero occurrences of the digit group, right there at the beginning of the string! So it dutifully replaces nothing with nothing twice, separated by a space. The result is the string " foo bar".
 
1 members found this post helpful.
Old 12-15-2017, 03:13 AM   #3
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Original Poster
Rep: Reputation: Disabled
Hi.

Thanks for the reply.

So 'nothing' is 'something' ?

That's what confused me there.
 
Old 12-15-2017, 03:18 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
Nothing certainly is the same as "zero or more". To be specific, it's zero.

And once sed has found what you've told it to look for, it won't look again without the greedy operator. That's why it turned "abc 123" into " abc 123", because the nothing between the start of the string and the first "a" matches '[0-9]*' perfectly.
 
1 members found this post helpful.
Old 12-15-2017, 03:21 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by Reprovo
So 'nothing' is 'something' ?
No - the fact that the "nothing" pattern matched is significant ("something" if you wish).
Unless you have a *VERY* good reason - and you have justification for it - don't use "*" in a pattern, but (almost) always prefer "+".
Make sure you understand the difference.

Last edited by syg00; 12-15-2017 at 03:22 AM. Reason: added quote for clarity
 
1 members found this post helpful.
Old 12-15-2017, 03:21 AM   #6
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Original Poster
Rep: Reputation: Disabled
Thanks again.

I understand that bit but i don't understand why duplicating 'nothing' using the '&&' actually creates a 'space'. Why is 'nothing' turned into a 'space'
 
Old 12-15-2017, 03:22 AM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
Because you have a space between the two ampersands in the right hand side of the expression.
 
1 members found this post helpful.
Old 12-15-2017, 03:25 AM   #8
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Original Poster
Rep: Reputation: Disabled
Oh lol !

I didn't even think of that! Thank you.
 
  


Reply

Tags
regex, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse/ explain sed line command to understand what it does/ the logic varu0612 Programming 2 09-16-2012 09:47 AM
[SOLVED] Very new to Linux/Trying to understand the Sed command kayjay1 Linux - Newbie 2 02-27-2011 01:07 PM
in sed command , I have difficulties to understand /1/2 seanrobi Programming 9 08-08-2010 12:09 PM
Simple shell script - sed command Todd88 Programming 10 07-06-2008 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration