LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 09: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, 09:46 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
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 10:06 AM.
 
Old 07-02-2008, 10:03 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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, 10: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, 10:07 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 10:12 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 05:50 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 12:29 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration