LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed (https://www.linuxquestions.org/questions/linux-newbie-8/sed-324428/)

benobi 05-17-2005 12:45 PM

sed
 
I'm trying to understand these sed rules I found in a makefile, can anyone tell me if I'm reading them correctly?

Code:

sed -e 's/#.*//'
says, substitute #.* with blanks.

Code:

sed -e 's/^[^:]*: *//'
This one eludes me. I know ^ matches a character at the beginning of a line. So I think the first half says, substitute any character that isn't a colon that appears at the beginning of a line. But then what's the *: * part that follows it?

Code:

sed -e 's/ *\\$$//'
Not sure about this one either. I think this says to substitute the text string "*\\$$" with nothing?

Code:

sed -e '/^$$/ d'
Delete each line that begins with $$ string?

Code:

sed -e 's/$$/ :/'
I'm thinking: Substitute a $ character at the end of a line with a :

cathectic 05-17-2005 01:49 PM

From http://www.amk.ca/python/howto/regex...metacharacters:
Quote:

$
Matches at the end of a line, which is defined as either the end of the string, or any location followed by a newline character.

homey 05-17-2005 01:53 PM

Quote:

I found in a makefile
Sounds more like a make homework file :)
Anyway keep plugging away at it, you aren't doing too bad.

benobi 05-17-2005 02:00 PM

It's not my homework... :p My friend told me to look at this makefile for this program someone else wrote and I can't seem to figure it out. If $ is end of line why are there two $'s in a row? Does that mean the end of the end of a line? What about ^$? Does this mean beginning of the end of the line?

homey 05-17-2005 02:22 PM

Ok, so what I do is create a small file.txt with examples in it and then run the code to see what it does. For example...
file.txt
Code:

now is the time
$
$$
for all good
men to come to
the aid of thier

Code:

cat file.txt | sed -e '/^$$/ d'
now is the time
$$
for all good
men to come to
the aid of thier

So what that does is remove the line which starts ( ^ ) and ends ( $ ) with a $

benobi 05-17-2005 02:48 PM

Oh ok, how come the $$ line didn't get deleted? It starts with a $ and ends with a $.

homey 05-17-2005 03:04 PM

The first line only had one $ and that is what the sed was told to look for.
If you want more than one of something, you would use a *
For example...
Code:

cat file.txt | sed -e '/^$*$/ d'
Now this example looks for a line which has a # in it and replaces the area from the # on with a blank.
If the # happens at the beginning of the line, you get a blank line.
Code:

cat file.txt | sed -e 's/#.*//'

benobi 05-17-2005 03:23 PM

Ok i understand $*$ and #.* now.

I'm still having trouble grasping ^$$

I have this in a text file:
Code:

$hello
$hello$
hello$
$$hello

and i run:
Code:

cat temp.txt | sed -e 's/^$$//'
and i get:
Code:

$hello
$hello$
hello$
$$hello

How come none of those got removed?

homey 05-17-2005 03:44 PM

cat temp.txt | sed -e 's/^$$//' is looking for a line that only has one $ and nothing else


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