if construct question

This is a discussion on if construct question within the awk forums in Programming Languages category; Hello, I'm trying to write a script to edit the gdm.conf file in order to change the Welcome= line. I was trying: if ( $0~/^Welcome/) {print "Welcome=New welcome message"} which works, but I can't quite seem to get the correct syntax to say if there isn't a "^Welcome=" line, add one. Any suggestions? Thanks! Brad...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-11-2008, 02:36 PM
brad.vanorden@gmail.com
Guest
 
Default if construct question

Hello,

I'm trying to write a script to edit the gdm.conf file in order to
change the Welcome= line. I was trying:

if ( $0~/^Welcome/) {print "Welcome=New welcome message"}

which works, but I can't quite seem to get the correct syntax to say
if there isn't a "^Welcome=" line, add one. Any suggestions?

Thanks!

Brad
Reply With Quote
  #2  
Old 08-11-2008, 02:59 PM
Hermann Peifer
Guest
 
Default Re: if construct question

brad.vanorden@gmail.com wrote:
> Hello,
>
> I'm trying to write a script to edit the gdm.conf file in order to
> change the Welcome= line. I was trying:
>
> if ( $0~/^Welcome/) {print "Welcome=New welcome message"}
>
> which works, but I can't quite seem to get the correct syntax to say
> if there isn't a "^Welcome=" line, add one. Any suggestions?
>


I guess your working code is somewhere from the middle of a rule.

A simple solution for adding a line at the end would be:

....
if ( $0~/^Welcome/) {print "Welcome=New welcome message" ; seen=1}
....
END {if (! seen) print "Welcome=New welcome message"}

Hermann
Reply With Quote
  #3  
Old 08-11-2008, 02:59 PM
brad.vanorden@gmail.com
Guest
 
Default Re: if construct question

On Aug 11, 2:36 pm, brad.vanor...@gmail.com wrote:
> Hello,
>
> I'm trying to write a script to edit the gdm.conf file in order to
> change the Welcome= line. I was trying:
>
> if ( $0~/^Welcome/) {print "Welcome=New welcome message"}
>
> which works, but I can't quite seem to get the correct syntax to say
> if there isn't a "^Welcome=" line, add one. Any suggestions?
>
> Thanks!
>
> Brad


Came up with the answer. In the BEGIN section, set a variable to 0.
If the pattern matches, change it to 1. In the END section, test if
variable is 0. If so, print the new welcome message.

Regards,

Brad
Reply With Quote
  #4  
Old 08-11-2008, 03:04 PM
Hermann Peifer
Guest
 
Default Re: if construct question

brad.vanorden@gmail.com wrote:
> In the BEGIN section, set a variable to 0.


You can just skip this part. Awk does it for you.

Hermann
Reply With Quote
  #5  
Old 08-11-2008, 09:03 PM
Bob Harris
Guest
 
Default Re: if construct question

In article
<7635edff-946e-4749-9d91-91253030932c@w7g2000hsa.googlegroups.com>
,
brad.vanorden@gmail.com wrote:

> Hello,
>
> I'm trying to write a script to edit the gdm.conf file in order to
> change the Welcome= line. I was trying:
>
> if ( $0~/^Welcome/) {print "Welcome=New welcome message"}
>
> which works, but I can't quite seem to get the correct syntax to say
> if there isn't a "^Welcome=" line, add one. Any suggestions?
>
> Thanks!
>
> Brad


awk '
BEGIN { print "Welcome=New welcome message" }
! /^Welcome=/ { print } # print anything expect a welcome
' gdm.conf >new.gdm.conf
Reply With Quote
  #6  
Old 08-11-2008, 10:47 PM
Ed Morton
Guest
 
Default Re: if construct question



On 8/11/2008 1:36 PM, brad.vanorden@gmail.com wrote:
> Hello,
>
> I'm trying to write a script to edit the gdm.conf file in order to
> change the Welcome= line. I was trying:
>
> if ( $0~/^Welcome/) {print "Welcome=New welcome message"}
>
> which works, but I can't quite seem to get the correct syntax to say
> if there isn't a "^Welcome=" line, add one. Any suggestions?
>
> Thanks!
>
> Brad


awk 'BEGIN{print "Welcome=New welcome message"} /^Welcome/{next}' file

Ed.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:32 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.