Re: Need help with SED


Subject: Re: Need help with SED
From: Arthur Corliss (arthur@corlissfamily.org)
Date: Wed May 01 2002 - 17:20:49 AKDT


On Wed, 1 May 2002, James McMorris wrote:

>
> Sorry, I don't understand what the "escaping newlines" means or what
> the pipe does. Here's what I have for the "meat" of my script so far:

What I meant is if you want more than one line to be inserted/appended, you
just need to add a '\' to the end of all the lines but the last.

> #----------------Start Code--------------
> #!/bin/sh
> sed -e '1421i\
> acl SSL_ports port 81 10000' /home/squid.conf > /home/squid.tmp
> sed -e '1421i\
> acl Safe_ports port 81 10000' /home/squid.tmp > /home/squid.conf
> sed -e '1421i\
> #SSL connections thru Squid for Webconfig and Webmin' /home/squid.conf
> > /home/squid.tmp
> sed -e '1421i\
> #The following two lines were added to the ACL section to
> enable' /home/squid.tmp > /home/squid.conf
> sed -e '1421i\
> ' /home/squid.conf > /home/squid.tmp
>
> cp /home/squid.tmp /home/squid.conf
> #--------------End Code--------------------
>
> Two questions:
> 1) Is there a more efficient way of inserting these lines rather than
> copying back-and-forth between these two files?

Yes, do it all in one stream:

sed '1421a \
#The following two lines were added to the ACL section to enable \
#SSL connections thru Squid for Webconfig and Webmin \
acl Safe_ports port 81 10000 \
acl SSL_ports port 81 10000
' /home/squid.conf > /home/squid.tmp
cp /home/squid.tmp /home/squid.conf

> 2) Is my code in the correct "format"? I want to learn the "right" way
> the first time. (Visions of spaghetti-code BASIC on a TI-994a from
> 1982 are dancing in my head. :)

Syntactically, it's correct. It would probably be much easier to use 'a' for
append, rather than 'i', so that you didn't have to add the lines in reverse,
though.

        --Arthur Corliss
          Bolverk's Lair -- http://arthur.corlissfamily.org/
          Digital Mages -- http://www.digitalmages.com/
          "Live Free or Die, the Only Way to Live" -- NH State Motto

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Wed May 01 2002 - 18:15:09 AKDT