Unix Shell - Deleting a line from a file and replacing a variable

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Deleting a line from a file and replacing a variable





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Deleting a line from a file and replacing a variable
Charles A. Landemaine

2006-11-30, 1:33 am

Hello, I need to open a file, to search for a line that starts with
lighttpd_enable="YES", to remove this line and to save the file.

Also I need to open another file, to search for "lighttpd-1.4.12"
everywhere in the file (it appears several times), to change this
string to "lighttpd-1.4.13" and to save the file.

How could I do that using sed? I'm using regular Bourne shell on
FreeBSD. Thanks in advance.

Chris F.A. Johnson

2006-11-30, 1:33 am

On 2006-11-30, Charles A. Landemaine wrote:
> Hello, I need to open a file, to search for a line that starts with
> lighttpd_enable="YES", to remove this line and to save the file.


grep -v '^lighttpd_enable="YES"' FILE > tempfile &&
mv -- tempfile FILE

If there may be more than one instance, and you only want to
remove the first:

awk '/^lighttpd_enable="YES"/ && x++ == 0 { next }
{ print }' FILE > tempfile && mv -- tempfile FILE

> Also I need to open another file, to search for "lighttpd-1.4.12"
> everywhere in the file (it appears several times), to change this
> string to "lighttpd-1.4.13" and to save the file.
>
> How could I do that using sed?


sed 's/lighttpd-1.4.12/lighttpd-1.4.13/g' FILE > tempfile &&
mv -- tempfile FILE

> I'm using regular Bourne shell on FreeBSD.


FreeBSD doesn't have a Bourne shell; /bin/sh is a POSIX shell.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Charles A. Landemaine

2006-11-30, 1:17 pm

Thanks Chris! Perfect, I add these tips in the PC-BSD knowledge
database if you don't mind

http://faqs.pcbsd.org/19_327_en.html
http://faqs.pcbsd.org/19_329_en.html

Cheers,

Charles.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com