Sed - parsing question

Sed - parsing question

Post by David Kenne » Wed, 08 Jan 1997 04:00:00



I have been trying to use 'sed' (for the first time!) to
search and replace '1' if and only if it appears after
[A-H]. ie replace A1, B1, C1, D1 etc to give A<new> B<new> C<new>

I have used the following command in 'vi':
        :%s/^\([A-H]\)1/\1-27.3/
(where -27.3 is the replace string)

As I want to do this for a sequence of numbers and for a number of files
I thought I'd convert the above (given to me) for use in a 'sed' program.
The problem is that I cannot get it to work.
Anyone know how this should be parsed for 'sed'?
I have tried things like s/^\([A-H]\)1/\1-27.3/g etc but just get
more confused!

Please reply by email and posting, my newserver is flakey...
Thanks very much.
--
David Kennedy, The Queen's University of Belfast.

 
 
 

Sed - parsing question

Post by Harrison Berger » Thu, 09 Jan 1997 04:00:00



 >I have been trying to use 'sed' (for the first time!) to
 >search and replace '1' if and only if it appears after
 >[A-H]. ie replace A1, B1, C1, D1 etc to give A<new> B<new> C<new>
 >
 >I have used the following command in 'vi':
 >   :%s/^\([A-H]\)1/\1-27.3/
 >(where -27.3 is the replace string)
 >

sed 's/[A-H]1/-27.3/g' file

This will also change things like A11 to -27.31, so if you don't
want that or A1C to come out as -27.3C you'll need to put that
in too.

sed 's/[A-H]1\([^0-9]\)/-27.3\1/g' file

will replace A1A with -27.3A/ , for example but A11A will
come out as A11A. You probably were not using the g if you
were only getting the first match in a line. stuffing in
the g says do it globally.

--

    Steinberger:            
State of the Instrument    

 
 
 

Sed - parsing question

Post by Bill Marc » Thu, 09 Jan 1997 04:00:00



>I have been trying to use 'sed' (for the first time!) to
>search and replace '1' if and only if it appears after
>[A-H]. ie replace A1, B1, C1, D1 etc to give A<new> B<new> C<new>

>I have used the following command in 'vi':
>    :%s/^\([A-H]\)1/\1-27.3/
>(where -27.3 is the replace string)

>As I want to do this for a sequence of numbers and for a number of files
>I thought I'd convert the above (given to me) for use in a 'sed' program.
>The problem is that I cannot get it to work.
>Anyone know how this should be parsed for 'sed'?
>I have tried things like s/^\([A-H]\)1/\1-27.3/g etc but just get
>more confused!

That should work.  You might have to put it in single quotes so that the
shell doesn't interpret special characters such as [ ] and ^ .
sed 's/^\([A-H]\)1/\1-27.3/g' oldfile > newfile

--

If you crossed Tickle Me Elmo with that Cabbage Patch doll, would you get
a Bite Me Elmo?

 
 
 

Sed - parsing question

Post by Brian S Hil » Thu, 09 Jan 1997 04:00:00


: I have been trying to use 'sed' (for the first time!) to
: search and replace '1' if and only if it appears after
: [A-H]. ie replace A1, B1, C1, D1 etc to give A<new> B<new> C<new>
: I have used the following command in 'vi':
:       :%s/^\([A-H]\)1/\1-27.3/
: (where -27.3 is the replace string)
: As I want to do this for a sequence of numbers and for a number of files
: I thought I'd convert the above (given to me) for use in a 'sed' program.
: The problem is that I cannot get it to work.
: Anyone know how this should be parsed for 'sed'?
: I have tried things like s/^\([A-H]\)1/\1-27.3/g etc but just get
: more confused!

Ah yes... this certainly takes me back to those years of frustration
and angst before I could say I had mastered sed.

: Please reply by email and posting, my newserver is flakey...
: Thanks very much.
: --
: David Kennedy, The Queen's University of Belfast.

[CC: courtesy email]

You are on the right track, but be reminded that constraining the regular
expression to BOL will certainly conflict with your intention to globally
substitute on every occurence on the line!

Turn s/^\([A-H]\)1/\1-27.3/g into s/\([A-H]\)1/\1-27.3/g (I.E. no caret).

-Brian
--
   ,---.     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.  
  /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \  

__,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.__

 
 
 

1. SED question, parsing strings

Hi.  I have a small ASCII text file (465 lines) of staff logon info
including email addresses for those who want to be contacted by email.
Each field is delimited by the pipe character and the file looks
something like this:


smith|mary|19981012=|Mary|Smithers|no

and so on.  465 records respectively.  As you can see, some people
have an email address after the equals sign and some people do not.

What I'm curious about doing in UNIX (SED, I imagine, or maybe GAWK or
Perl), is how to parse just the email address per line, as well as
ignoring lines that have no email address.  All lines are delimited by
the pipe character respectively and all email addresses are preceded
by the equals sign respectively -- except for those with no email
address given, in which case the equals sign is followed by the pipe
delimiter.  The resulting file would look something like this:



and so on.  A complete list of emai addresses provided by the staff
members who want to receive info via email.  I'm trying various SED
strings and seem to be getting close but so far, having hit on the
right string.  Any suggestions or assistance would be most
appreciated.  Thanks.

Bob

If emailing, please remove _NOJUNK from my address.  Thanx.

2. how to change the LANG envirement value for the ftpd server

3. parsing a 'flexible' text file with SED: i'm in over my head ....

4. Debian 1.2: Kernel Panic at setup

5. using sed to parse numeric and non-numeric

6. Freashmeat like code

7. Sed: Parsing out chunks of a file

8. Linux on Public radio

9. SED: parsing a 'flexible' text file ... in over my head!

10. Parsing a string using awk/sed/ or unix shell commands

11. parsing a string in sed/awk/????

12. title parse with sed

13. wanted: sed (or other) script to parse a large file into smaller ones