Replace text block with sed?

Replace text block with sed?

Post by JustBob4.. » Wed, 12 May 2004 23:15:22



I'm trying to perform a simple find and replace with sed, but it's
making my head hurt. I need to locate the following block in a text
file:

[ req ]
default_bits            = 1024
default_keyfile         = privkey.pem
distinguished_name      = req_distinguished_name
attributes              = req_attributes
x509_extensions = v3_ca

dirstring_type = nobmp

[ req_distinguished_name ]
countryName                     = Country Name (2 letter code)
countryName_default             = AU
countryName_min         = 2
countryName_max         = 2

localityName                    = Locality Name (eg, city)

organizationalUnitName          = Organizational Unit Name (eg,
section)

commonName                      = Common Name (eg, YOUR name)
commonName_max          = 64

emailAddress                    = Email Address
emailAddress_max                = 40

[ req_attributes ]
challengePassword               = A challenge password
challengePassword_min           = 4
challengePassword_max           = 20

... and replace it with this:

[ req ]
default_bits            = 1024
default_keyfile         = keyfile.pem
distinguished_name      = req_distinguished_name
attributes              = req_attributes
x509_extensions         = v3_ca
prompt                  = no

[ req_distinguished_name ]
C                       = GB
ST                      = Test State or Province
L                       = Test Locality
O                       = Organization Name
OU                      = Organizational Unit Name
CN                      = Common Name

[ req_attributes ]
challengePassword               = A challenge password

Surely I don't have to do a line-by-line replacement. It seems like I
should be able to pick a beginning and ending element, and choose to
replace everything in between... I don't know how to deal with a block
in sed, though...

 
 
 

Replace text block with sed?

Post by Alan Conno » Thu, 13 May 2004 03:22:45



> I'm trying to perform a simple find and replace with sed, but it's
> making my head hurt. I need to locate the following block in a text
> file:

> [ req ]
> default_bits               = 1024
> default_keyfile    = privkey.pem
> distinguished_name = req_distinguished_name
> attributes         = req_attributes
> x509_extensions    = v3_ca

> dirstring_type = nobmp

> [ req_distinguished_name ]
> countryName                        = Country Name (2 letter code)
> countryName_default                = AU
> countryName_min            = 2
> countryName_max            = 2

> localityName                       = Locality Name (eg, city)

> organizationalUnitName             = Organizational Unit Name (eg,
> section)

> commonName                 = Common Name (eg, YOUR name)
> commonName_max             = 64

> emailAddress                       = Email Address
> emailAddress_max           = 40

> [ req_attributes ]
> challengePassword          = A challenge password
> challengePassword_min              = 4
> challengePassword_max              = 20

> ... and replace it with this:

> [ req ]
> default_bits               = 1024
> default_keyfile    = keyfile.pem
> distinguished_name = req_distinguished_name
> attributes         = req_attributes
> x509_extensions            = v3_ca
> prompt                     = no

> [ req_distinguished_name ]
> C                  = GB
> ST                 = Test State or Province
> L                  = Test Locality
> O                  = Organization Name
> OU                 = Organizational Unit Name
> CN                 = Common Name

> [ req_attributes ]
> challengePassword          = A challenge password

> Surely I don't have to do a line-by-line replacement. It seems like I
> should be able to pick a beginning and ending element, and choose to
> replace everything in between... I don't know how to deal with a block
> in sed, though...

Here's the basic recipe, but you need to put it in a file and call that
file with:

sed -f file  inputfile > tempfile

After testing:

mv tempfile inputfile

---------------------------------- remove this line
/pattern1/,/pattern2/c\
line1.......\
line2........\
linelast....
----------------------------------remove this line

No spaces after the "\" and no blank lines. Doesn't need to be executable.

The "c" means "change".

AC

--
Pass-List -----> Block-List ----> Challenge-Response
The key to taking control of your mailbox.  Design Parameters:
http://tinyurl.com/2t5kp ||   http://tinyurl.com/3c3ag
Challenge-Response links -- http://tinyurl.com/yrfjb

 
 
 

Replace text block with sed?

Post by rakesh shar » Thu, 13 May 2004 13:24:44



> I'm trying to perform a simple find and replace with sed, but it's
> making my head hurt. I need to locate the following block in a text
> file:

> [ req ]
> default_bits               = 1024
> default_keyfile    = privkey.pem
> distinguished_name = req_distinguished_name
> attributes         = req_attributes
> x509_extensions    = v3_ca

> dirstring_type = nobmp

> [ req_distinguished_name ]
> countryName                        = Country Name (2 letter code)
> countryName_default                = AU
> countryName_min            = 2
> countryName_max            = 2

> localityName                       = Locality Name (eg, city)

> organizationalUnitName             = Organizational Unit Name (eg,
> section)

> commonName                 = Common Name (eg, YOUR name)
> commonName_max             = 64

> emailAddress                       = Email Address
> emailAddress_max           = 40

> [ req_attributes ]
> challengePassword          = A challenge password
> challengePassword_min              = 4
> challengePassword_max              = 20

> ... and replace it with this:

> [ req ]
> default_bits               = 1024
> default_keyfile    = keyfile.pem
> distinguished_name = req_distinguished_name
> attributes         = req_attributes
> x509_extensions            = v3_ca
> prompt                     = no

> [ req_distinguished_name ]
> C                  = GB
> ST                 = Test State or Province
> L                  = Test Locality
> O                  = Organization Name
> OU                 = Organizational Unit Name
> CN                 = Common Name

> [ req_attributes ]
> challengePassword          = A challenge password

> Surely I don't have to do a line-by-line replacement. It seems like I
> should be able to pick a beginning and ending element, and choose to
> replace everything in between... I don't know how to deal with a block
> in sed, though...

here's one way:

sed -e '
   /\[ req ]/,/^$/{
     /^$/s/^/\
prompt                  = no/
     b
   }

   /\[ req_distinguished_name ]/,/^$/c\
C                       = GB\
ST                      = Test State or Province\
L                       = Test Locality\
O                       = Organization Name\
OU                      = Organizational Unit Name\
CN                      = Common Name\

\

    b

    /\[ req_attributes ]/,/^$/c\
challengePassword               = A challenge password\
\

' yourfile

 
 
 

1. sed - replacing a block of text with a line.

Hello.

I would like to be able to use sed for a substitute operation.
I would like the script or command to iterate through all
files in a dir, removing a  block of text and replacing it
with a single line.

The files in question are html files for a web site.
The blocks are stylesheets, that I wish to replace
with a link to an external stylesheet.

Unfortunalty, I am new to sed and regex.
I couldn't find anything obvious in the sed manuals
that shows how to perform a substitute operation
from multiple lines to a single line.

I gratefully welcome any 'RTFM' pointers, that will direct
me to points of relevance I can use to craft a solution.
I even more gratefully welcome accompanying solutions that
I can learn from.

I am using gnu sed version 3.02

Thank you.

2. Permissions on Apache 1.3.9

3. Sed: Replace text in one file with SPECIFIC text from another

4. Return address with "mail"

5. sed command to replace text with blank spaces

6. ANNOUNCE: tkWorld 1.1.0 Release

7. How to replace string with some text (with new lines) sed or something else ?

8. Suggested Hardware for Linux Router

9. How to replace a whole line (matching a pattern) in a text filke with SED ?

10. sed not replacing text

11. Sed command to replace text in several files

12. Sed: replacing text with textfile content

13. replacing a block of text in a document