LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed misbehaving? (https://www.linuxquestions.org/questions/programming-9/sed-misbehaving-388534/)

linmix 12-02-2005 02:47 AM

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


keefaz 12-02-2005 04:02 AM

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)

eddiebaby1023 12-02-2005 03:19 PM

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.

linmix 12-02-2005 03:47 PM

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.

keefaz 12-04-2005 07:09 AM

That should be :
Code:

sed -e 's/\([^ ]\+\)\( \+\)\([^ ]\+\)/\3\2\1/g'
\([^ ]\+\) : match a word
\( \+\) : match one or more space

linmix 12-04-2005 07:19 AM

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.

keefaz 12-04-2005 07:22 AM

1. no idea, anybody can make mistake, it is a human feature :p
2. Open a terminal and type : info sed

linmix 12-04-2005 07:35 AM

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 :)

eddiebaby1023 12-04-2005 08:44 AM

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.

linmix 12-04-2005 10:43 AM

So imagine I want sed to return 'linmix' .. how would I go about it?


All times are GMT -5. The time now is 06:37 AM.