Re: sed help

2009-06-17 Thread Giorgos Keramidas
On Wed, 17 Jun 2009 10:55:28 -0700 (PDT), chloe K  wrote:
> Hi
> I have a file. list.txt (two columns)
>  
> column1    column2
> name    address
>  
> I need to put in the letter file letter.txt eg:
>  
> Dear: Chloe
> Address: CA
>  
> Can I use this
>  
> for i `cat list.txt` | sed 's/Chloe/$i.1; /CA/$i.2/g' $i.letter.txt

No that won't work.  sed does 'stream editing' to its own input file, so
you have to redirect each output for *every* loop iteration.  But I
don't think this is a good method of solving this problem, because you
only have one input file and one output file.

See what the following does, to give you can idea:

$ echo giorgos keram...@ceid.upatras.gr | sed -e 's/^\([^ ]*\)[ ]*\(.*\)$/\
Dear:\1\
Address: \2\
/'

NOTE: If you really want to work effectively with sed, please take a bit
of time to read the manpage of sed(1) and ed(1), paying careful to the
parts about: (1) regular expressions, (2) character classes, and (3) the
rules of character quoting.

It's also worth noting that you don't _have_ to use sed for this
specific problem, because there are other tools more suitable for
processing data in columns, i.e. awk(1):

$ echo giorgos keram...@ceid.upatras.gr | \
awk '{print "Dear:   ", $1; print "Address:", $2}'
Dear:giorgos
Address: keram...@ceid.upatras.gr

A single line of awk is vastly more readable than the equivalent sed
expression in this case.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: sed help

2009-06-17 Thread Dan Nelson
In the last episode (Jun 17), chloe K said:
> I have a file. list.txt (two columns)
>  
> column1    column2
> name    address
>  
> I need to put in the letter file letter.txt eg:
>  
> Dear: Chloe
> Address: CA
>  
> Can I use this 
>  
> for i `cat list.txt` | sed 's/Chloe/$i.1; /CA/$i.2/g' $i.letter.txt

Try:

cat list.txt | while read name address ; do
  sed -e "s/Chloe/$name/ ; s/CA/$address/" < letter.txt > letter.$name.txt
done

You need the "while read" part so that you loop once per line.  Your code
would have looped once per word in the input file.  You also need
double-quotes on your sed line because $variable expansion isn't done inside
single-quotes.  If your names have spaces in them, consider swapping the
name and address in your input file, since "read" splits on spaces and
assigns the remainder of the line to the last variable listed.


-- 
Dan Nelson
dnel...@allantgroup.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"


Re: Sed Help.....

2004-11-16 Thread Loren M. Lang
On Thu, Nov 11, 2004 at 08:12:55AM +0200, Giorgos Keramidas wrote:
> On 2004-11-10 15:15, Rod Person <[EMAIL PROTECTED]> wrote:
> > On Wednesday 10 November 2004 7:58 pm, mailing lists at MacTutor wrote:
> > > Take a look at what the shell replacement is actually doing. If you
> > > were to write the line manually it would look like this:
> > >
> > > sed -e 's/\/usr\X11R6\/bin\/xdm/\/usr\/local\/bin\/kdm/g' ...
> > >
> > > Right?
> > >
> > > But the shell doesn't escape the path separators (slashes). You need to
> > > escape them yourself in the variable assignments. Like this,
> > >
> > > KDMLINE='\/usr\/local\/bin\/kdm'
> > > &c
> >
> > I hate when you look at something for hours and it something you know you
> > should have known! I had at one point had the variables with double qoute 
> > and
> > even tried to escape the qoutes!!

I'd recommend using the : because it's the least likely character to
appear in a filename since : is the seperator used in $PATH.  But then
again, who would use a | in a filename!?

> 
> You can also use different sed-separator characters:
> 
>   sed -e "s|${REPLACELINE}|${KDMLINE}|"
> 
> The choise of '|' is arbitrary above.  It could have been '@', '#', or
> '!', for all that sed(1) cares.  The substitution would still work.
> 
> ___
> [EMAIL PROTECTED] mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "[EMAIL PROTECTED]"

-- 
I sense much NT in you.
NT leads to Bluescreen.
Bluescreen leads to downtime.
Downtime leads to suffering.
NT is the path to the darkside.
Powerful Unix is.

Public Key: ftp://ftp.tallye.com/pub/lorenl_pubkey.asc
Fingerprint: B3B9 D669 69C9 09EC 1BCD  835A FAF3 7A46 E4A3 280C
 
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Sed Help.....

2004-11-10 Thread Giorgos Keramidas
On 2004-11-10 15:15, Rod Person <[EMAIL PROTECTED]> wrote:
> On Wednesday 10 November 2004 7:58 pm, mailing lists at MacTutor wrote:
> > Take a look at what the shell replacement is actually doing. If you
> > were to write the line manually it would look like this:
> >
> > sed -e 's/\/usr\X11R6\/bin\/xdm/\/usr\/local\/bin\/kdm/g' ...
> >
> > Right?
> >
> > But the shell doesn't escape the path separators (slashes). You need to
> > escape them yourself in the variable assignments. Like this,
> >
> > KDMLINE='\/usr\/local\/bin\/kdm'
> > &c
>
> I hate when you look at something for hours and it something you know you
> should have known! I had at one point had the variables with double qoute and
> even tried to escape the qoutes!!

You can also use different sed-separator characters:

sed -e "s|${REPLACELINE}|${KDMLINE}|"

The choise of '|' is arbitrary above.  It could have been '@', '#', or
'!', for all that sed(1) cares.  The substitution would still work.

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Sed Help.....

2004-11-10 Thread Chuck Robey
On Thu, 11 Nov 2004, Svein Halvor Halvorsen wrote:

>
> [Rod Person, 2004-11-10]
> >  Here is the relavent code:
> >
> >  KDMLINE="/usr/local/bin/kdm -nodaemon"
> >  REPLACELINE="/usr/X11R6/bin/xdm -nodaemon"
> >
> >  sed -e "s/$REPLACELINE/$KDMLINE/g" /etc/test/ttys > /etc/test/new
> >
> >  Here is the error I'm getting:
> >  sed: 1: "s//usr/X11R6/bin/xdm -n ...": bad flag in substitute command: 
> > 'X'
> >
>
>
> I was just made aware of a really nice feature of sed just yesterday:
>
> You can replace the / as a seperator with whatever character you want, as
> long as you use that same character all the way!!
>
> This is really nice, since you wouldn't need to escape the / characters as
> you otherwise would need to do.

Works this way with just about any system using the sed substitution
syntax (for instance, vi uses it, vim does).  Go looking at nearly any
large Gmake script (sucu as one of the makefiles from gcc) and you'll see
it all over the place.

>
> Just do like this (for instance):
>
> sed -2 s_$REPLACELINE_$KDMLIME_g /etc/test/ttys > /etc/test/new
> ___
> [EMAIL PROTECTED] mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "[EMAIL PROTECTED]"
>


Chuck Robey | Interests include C & Java programming, FreeBSD,
[EMAIL PROTECTED]   | electronics, communications, and SF/Fantasy.

New Year's Resolution:  I will not sphroxify gullible people into looking up
fictitious words in the dictionary (on the wall at my old fraternity,
Signa Phi Nothing).

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Sed Help.....

2004-11-10 Thread Svein Halvor Halvorsen

[Rod Person, 2004-11-10]
>  Here is the relavent code:
>  
>  KDMLINE="/usr/local/bin/kdm -nodaemon"
>  REPLACELINE="/usr/X11R6/bin/xdm -nodaemon"
>  
>  sed -e "s/$REPLACELINE/$KDMLINE/g" /etc/test/ttys > /etc/test/new
>  
>  Here is the error I'm getting:
>  sed: 1: "s//usr/X11R6/bin/xdm -n ...": bad flag in substitute command: 
> 'X'
>  


I was just made aware of a really nice feature of sed just yesterday: 

You can replace the / as a seperator with whatever character you want, as 
long as you use that same character all the way!!

This is really nice, since you wouldn't need to escape the / characters as 
you otherwise would need to do.

Just do like this (for instance):

sed -2 s_$REPLACELINE_$KDMLIME_g /etc/test/ttys > /etc/test/new
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Sed Help.....

2004-11-10 Thread Volker Eckert
On Wed, Nov 10, 2004 at 02:43:01PM +, Rod Person wrote:
> 
> KDMLINE="/usr/local/bin/kdm -nodaemon"
> REPLACELINE="/usr/X11R6/bin/xdm -nodaemon"
> 
> sed -e "s/$REPLACELINE/$KDMLINE/g" /etc/test/ttys > /etc/test/new
> 
> Here is the error I'm getting:
> sed: 1: "s//usr/X11R6/bin/xdm -n ...": bad flag in substitute command: 'X'
   ^^^

my guess is:

after expanding $REPLACELINE your command looks like you see above,
which is not what you wanted.
proposed solution: use other delimiters for sed.
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Sed Help.....

2004-11-10 Thread Rod Person
On Wednesday 10 November 2004 7:58 pm, mailing lists at MacTutor wrote:
> Rod,
>
> Take a look at what the shell replacement is actually doing. If you
> were to write the line manually it would look like this:
>
> sed -e 's/\/usr\X11R6\/bin\/xdm/\/usr\/local\/bin\/kdm/g' ...
>
> Right?
>
> But the shell doesn't escape the path separators (slashes). You need to
> escape them yourself in the variable assignments. Like this,
>
> KDMLINE='\/usr\/local\/bin\/kdm'
> &c
>
> And if there is only one occurrence per line, then you don't need the
> 'g' modifier.
>
> Hope that helps.
>
> Alex


Thanks Alex and Miguel...

I hate when you look at something for hours and it something you know you 
should have known! I had at one point had the variables with double qoute and
even tried to escape the qoutes!!

Thanks again

Rod...

___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Sed Help.....

2004-11-10 Thread Miguel Mendez
On Wed, 10 Nov 2004 14:43:01 +
Rod Person <[EMAIL PROTECTED]> wrote:

Hi,

> KDMLINE="/usr/local/bin/kdm -nodaemon"
> REPLACELINE="/usr/X11R6/bin/xdm -nodaemon"
> 
> sed -e "s/$REPLACELINE/$KDMLINE/g" /etc/test/ttys > /etc/test/new
> 
> Here is the error I'm getting:
> sed: 1: "s//usr/X11R6/bin/xdm -n ...": bad flag in substitute
> command: 'X'
> 

Both strings contain /, so you have to either escape it "\/" or use
different command separator, e.g. '[EMAIL PROTECTED]@[EMAIL PROTECTED]'

Cheers,
-- 
Miguel Mendez <[EMAIL PROTECTED]> | Extreme ways are back again
http://www.energyhq.es.eu.org| Extreme places I didn't know
PGP Key: 0xDC8514F1  | I broke everything new again
Note: All HTML mail goes to /dev/null| Everything that I'd owned



pgpfuuM5YtCZw.pgp
Description: PGP signature


Re: Sed Help.....

2004-11-10 Thread mailing lists at MacTutor
Rod,
Take a look at what the shell replacement is actually doing. If you 
were to write the line manually it would look like this:

sed -e 's/\/usr\X11R6\/bin\/xdm/\/usr\/local\/bin\/kdm/g' ...
Right?
But the shell doesn't escape the path separators (slashes). You need to 
escape them yourself in the variable assignments. Like this,

KDMLINE='\/usr\/local\/bin\/kdm'
&c
And if there is only one occurrence per line, then you don't need the 
'g' modifier.

Hope that helps.
Alex
On Nov 10, 2004, at 9:43 AM, Rod Person wrote:
I trying to write a script that will make it easier for a friend that I
finally talked into trying Freebsd setup a desktop.
This part of the script is suppose to change the line in the ttys file
to allow kdm to start on boot.
Here is the relavent code:
KDMLINE="/usr/local/bin/kdm -nodaemon"
REPLACELINE="/usr/X11R6/bin/xdm -nodaemon"
sed -e "s/$REPLACELINE/$KDMLINE/g" /etc/test/ttys > /etc/test/new
Here is the error I'm getting:
sed: 1: "s//usr/X11R6/bin/xdm -n ...": bad flag in substitute 
command: 'X'

I can't seem to figure this out and the only things I've found in
searching is that sed had a problem with replacing strings over
4096 bytes, but I don't think this is the cause and all those post
were years old.
This is on FreeBSD 5.3.
Rod
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to 
"[EMAIL PROTECTED]"


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Alexander Sendzimir (owner)802 863 5502
 MacTutor: Apple Mac OS X Consulting   [EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"