grep vs. sed consistencies

grep vs. sed consistencies

Post by Don Turnbul » Mon, 09 Aug 1999 04:00:00



i'm running into this puzzler as i'm trying to searc (and eventually
replace) the related strings
"card=", "cards=", or for that matter"cardinals=".

this command works fine for grep:

    grep -e card*= lookfile.txt

but this doesn't work in sed:

  sed -e 's/card*=/replacestring/' lookfile.txt>newfile.txt

any ideas?

 
 
 

grep vs. sed consistencies

Post by Charles Dem » Tue, 10 Aug 1999 04:00:00




Quote:>i'm running into this puzzler as i'm trying to searc (and eventually
>replace) the related strings
>"card=", "cards=", or for that matter"cardinals=".

>this command works fine for grep:

>    grep -e card*= lookfile.txt

>but this doesn't work in sed:

>  sed -e 's/card*=/replacestring/' lookfile.txt>newfile.txt

>any ideas?

read the man pages

grep 'card.*=' lookfile.txt

and

sed -e 's/card.*=/replacestring' lookfile.txt > newfile.txt

both use regular expressions where .* represents any number of
characters.

I suspect your grep expression is not really doing what you
wanted it to do.

man grep
man sed

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.


 
 
 

grep vs. sed consistencies

Post by Donn Ca » Tue, 10 Aug 1999 04:00:00


| i'm running into this puzzler as i'm trying to searc (and eventually
| replace) the related strings
| "card=", "cards=", or for that matter"cardinals=".
|
| this command works fine for grep:
|
|     grep -e card*= lookfile.txt
|
| but this doesn't work in sed:
|
|   sed -e 's/card*=/replacestring/' lookfile.txt>newfile.txt

I wouldn't expect grep would work there very well either.  It will
match car=, card=, cardd=, carddd= etc., but it shouldn't match cards=.
Unquoted like that, it's not strictly predictable, but the versions
of grep I have at hand treat "d*" as any number of d's, including 0.
This obviously is a little different from the shell's *.

One fairly tight rule that allows the variations you mention would be

    sed 's/card[ainls]*=/replacestring/' lookfile.txt > newfile.txt

That allows any combination of a i n l and s between "card" and "=".
Just to show the regular expression * in a more clearly useful context,

    sed 's/card[ainls]* *=/replacestring/' lookfile.txt > newfile.txt

will match all the same lines and also those where one or more spaces
crept in before the "=".

        Donn Cave, University Computing Services, University of Washington

 
 
 

grep vs. sed consistencies

Post by Kurt J. Lanz » Tue, 10 Aug 1999 04:00:00



> i'm running into this puzzler as i'm trying to searc (and eventually
> replace) the related strings
> "card=", "cards=", or for that matter"cardinals=".

> this command works fine for grep:

>     grep -e card*= lookfile.txt

> but this doesn't work in sed:

>   sed -e 's/card*=/replacestring/' lookfile.txt>newfile.txt

> any ideas?

Your RE in the sed command reads "zero or more 'd' characters".
"man sed" and/or "man regexp" for a description of Regular
Expressions.
 
 
 

grep vs. sed consistencies

Post by bmar.. » Wed, 11 Aug 1999 04:00:00



   >i'm running into this puzzler as i'm trying to searc (and eventually
   >replace) the related strings
   >"card=", "cards=", or for that matter"cardinals=".
   >this command works fine for grep:
   >grep -e card*= lookfile.txt
   >but this doesn't work in sed:
   >sed -e 's/card*=/replacestring/' lookfile.txt>newfile.txt
   >any ideas?
Why are you crossposting this to alt.msdos.batch and comp.unix.shell?
If that grep command seems to work in Unix, it's because without quotes
the shell expands "card*=" into a list of matching filenames, then grep
would see the first of these as the search string to find in all the other
files.  If no filenames match the pattern "card*=", it is passed to grep,
where it will match "car=", "card=", "cardd=", etc.
man regexp

Net-Tamer V 1.08X - Test Drive

 
 
 

grep vs. sed consistencies

Post by John Savag » Fri, 20 Aug 1999 04:00:00


i'm running into this puzzler as i'm trying to searc (and eventually

Quote:>replace) the related strings
>"card=", "cards=", or for that matter"cardinals=".

Perhaps: sed 's/card[^=]*=/replace=/g' file
--
John Savage            (for email, replace "ks" with "k" and delete "n")
 
 
 

1. /bin/grep vs /usr/ucb/grep

Does anyone on the net know offhand what is the difference between
/bin/grep and /usr/ucb/grep?  Furthermore, does anyone know which
one the man page refers to?  This is on a vanilla 4.2bsd system.
Someone just walked into my office and asked the question, and I sure
wasn't able to answer it.

-curtis-
----
Curtis C. Generous

UUCP: {seismo,vrdxhq}!dgis!generous

2. sendmail 8.10 and FreeBSD 4.0

3. Linux vs OS2 vs NT vs Win95 vs Multics vs PDP11 vs BSD geeks

4. Mains Networking

5. grep sTerm * | grep -v grep ???

6. Unix-flavored intro pkg?

7. tail | grep | grep | grep

8. C for AIX ver. 4.4 - preprocessorproblems

9. sed sed sed

10. . 2 sed FAQs, sed exes, sed cetras; URLs

11. SED SED SED...

12. (sed 1q ; sed 2q) : no output from 2nd 'sed'

13. Grep/awk/sed for multiple/groups of lines in a file???