LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-02-2008, 08:34 AM   #1
vkmgeek
Member
 
Registered: Feb 2006
Location: Ahmedabad
Distribution: rhel5
Posts: 185
Blog Entries: 2

Rep: Reputation: 31
sed confusion


[root@localhost ~]# cat t1
viral
hello
hi
root@localhost ~]#
root@localhost ~]# cat t1 | sed 's/\(.*\)./\1t/'
virat
hellt
ht
[root@localhost ~]# cat t1 | sed 's/\(*\)./\1t/'
viral
hello
hi
[root@localhost ~]#

I am confused. There is only one difference in last two commands and that is one dot is extra cat t1 | sed 's/\("."*\)./\1t/' in second last command.

To me that "." (dot) should not have any impact on output, yet it has...

How ?? Why ??
 
Old 07-02-2008, 08:46 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Post deleted - didn't contribute like I had hoped.
(BTW, you don't need the "cat" but that's a different discussion)

Last edited by syg00; 07-02-2008 at 09:06 AM.
 
Old 07-02-2008, 09:03 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Try it on this:

viral
hello
hi
he*llo


n* means zero or more of n
* with nothing before means *
n\* means n folowed by *
 
Old 07-02-2008, 09:04 AM   #4
webaware
Member
 
Registered: Nov 2007
Location: Australia
Distribution: Fedora FC-9 x86-64+i686,CentOS 5.2 i686
Posts: 58

Rep: Reputation: 16
A '.' matches any single character. A '*' matches the preceding element zero or more times. Thus, '.*' matches any single character followed by zero or more 'any single character's.

Do a 'man perlre' for a quick primer on regular expressions. It's Perl-specific, but close enough to what sed uses.
 
Old 07-02-2008, 09:07 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I am missing the question.

I would assume that ".*" and "*" do not mean the same thing, but then I don't know what "*" means by itself.

What is the desired result?
 
Old 07-02-2008, 09:12 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by nx5000 View Post
* with nothing before means *
I didn't know that!!

Is it true of any of the "modifier" characters?---eg "+" or "?"

If "*" by itself is read as the literal value, then how is "\*" read?
 
Old 07-02-2008, 04:50 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quantifiers require an atom preceeding them to be quantifiers. Otherwise, they are non-special. \* follows the order of precidence that \ quotes a character, so the * is quoted to a literal *.
 
  


Reply



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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
WM Confusion phoeniXflame Slackware 3 02-16-2003 05:19 AM

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

All times are GMT -5. The time now is 06:42 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