LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-02-2005, 03:47 AM   #1
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
sed misbehaving?


I don't know if I'm doing something wrong here, but sed appears to be not working properly on my system. I tried a number of examples from Rute (asper my sig.) and whenever I try a sed command of the the 'sed -e 's/>regexp>/<substitution>/g' type nothing happens.

I tried the following command:

Code:
$ ls -l | grep ^-| sed -e 's/^\(<.*> [ ]*\){8}\(.$\)/\2/g'
It is supposed to return 'clean' filenames. The only thing I get is the ls -l output without any directories, but with full permission, owner, time etc info. I've asked other people to try the command and they tell me it works. What might be going wrong?
Code:
$ rpm -q sed 
sed-4.1.4-1
 
Old 12-02-2005, 05:02 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
What do you call 'clean' filenames ? The output of this command shows me files as :
-rw-r--r-- 1 keefaz users 60938 2005-11-14 00:06 divx2pass.log

(same for each files, no directory in output as the grep ^- explicitly discard them)

Last edited by keefaz; 12-02-2005 at 05:04 AM.
 
Old 12-02-2005, 04:19 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
You don't even need the sed because the pattern doesn't match. If you just want the filenames of regular files (assuming no spaces in the filenames)
Code:
ls -l | grep ^-| sed -e 's/.* //g'
will do it.
 
Old 12-02-2005, 04:47 PM   #4
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
Quote:
Originally posted by eddiebaby1023
Code:
ls -l | grep ^-| sed -e 's/.* //g'
will do it. [/B]
Wheyhey, that works! (that's what I meant with 'clean' filenames) Thanks.

But why does it work? Why doesn't it return 'linmix' as well if the ls -l output is '-rwxr-xr-x 1 linmix linmix 232 Dec 2 22:35 test.sh'

Also, the book I'm reading (RUTE > see sig.) gives the following example with supposed output:
Code:
$ sed -e 's/\(<[^ ]*>\)\([ ]*\)\(<[^ ]*>\)/\3\2\1/g'
GNU Linux is cool
Linux GNU cool is
However, when I try it the command doesn't alter the text in any way.
 
Old 12-04-2005, 08:09 AM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
That should be :
Code:
sed -e 's/\([^ ]\+\)\( \+\)\([^ ]\+\)/\3\2\1/g'
\([^ ]\+\) : match a word
\( \+\) : match one or more space
 
Old 12-04-2005, 08:19 AM   #6
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
Brilliant!

Quote:
Originally Posted by keefaz
That should be :
Code:
sed -e 's/\([^ ]\+\)\( \+\)\([^ ]\+\)/\3\2\1/g'
\([^ ]\+\) : match a word
\( \+\) : match one or more space
2 questions:
  1. Why would a command from an authoritative source like RUTE not be correct? Have there been changes in sed to justify the difference?
  2. I've seen number of tutorials, but if you can recommend one, I'd be much obliged.
 
Old 12-04-2005, 08:22 AM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
1. no idea, anybody can make mistake, it is a human feature
2. Open a terminal and type : info sed
 
Old 12-04-2005, 08:35 AM   #8
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
1. Well, the books ben around for +10 years and has been updated, plus it's used by quite some institutions to teach linux courses, so somebody might have noticed before now...

2. info sed has a lot of info, but i'm looking for a tutorial, something with practical examples to see what happens (or should happen). It's just the way I learn best
 
Old 12-04-2005, 09:44 AM   #9
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally Posted by linmix
Wheyhey, that works! (that's what I meant with 'clean' filenames) Thanks.

But why does it work? Why doesn't it return 'linmix' as well if the ls -l output is '-rwxr-xr-x 1 linmix linmix 232 Dec 2 22:35 test.sh'
Regular expression generally do what's knwon as "greedy matching" - they match as much as they possibly can. In the command above, sed will match zero or more occurrences of any character followed by a space. Greedy matching will match to the last space it can find, which is the one preceding the filename. Greedy matching is also the main pitfall you have to guard against when constructing complex patterns.
 
Old 12-04-2005, 11:43 AM   #10
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
So imagine I want sed to return 'linmix' .. how would I go about it?
 
  


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
A misbehaving ad - Firefox 1.0.7 Orkie LQ Suggestions & Feedback 3 10-27-2005 05:10 PM
ndiswrapper Broadcome wireless misbehaving... tahiche Linux - Wireless Networking 1 10-10-2005 10:46 AM
Cups misbehaving mjrich Debian 2 02-22-2005 05:11 PM
flashdrive misbehaving linmix Linux - Hardware 9 02-06-2005 05:32 AM
Why is my samba misbehaving? sapphos Linux - Networking 4 10-11-2003 03:27 PM


All times are GMT -5. The time now is 05:23 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration