The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-10-2006
nymus7 nymus7 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 47
Talking new in awk

Hi

The variable expdate could have three states:
1.empty
2.N/A
3.mm/dd/yyyy

To test this variable, I've made this script:


Code:
1.... |awk '
2.    BEGIN {FS=":  " 
3.    getline expdate <"ExpDate.txt"
4.    }
5.      print "expdate :"$expdate
6.      if ($expdate = "[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9]")
7.      { 
8.        print "expdate are :"$expdate
9.      }else{
10.        print "The are no expdate"
11.     }
12.     }

The print statement (line 5.) is right but the test not!
Have anyone an Idea where are my errors in line 6?
I want to check if the variable expdate is a date!
Are they an other way to reduce all the [0-9]?

In some books I have seen different examples for variable in test:
ex.: if (variable = 10) or if ($variable = 10)! But to see a content of a variable we must always use ($) Isn't it?

Thanks in advance for all your input!
-nm
  #2 (permalink)  
Old 04-10-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
echo "Hai" | nawk '
BEGIN {FS=": "
getline expdate <"ExpDate.txt";
}

{
print "expdate :"expdate
if (expdate ~ "[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]")
{
print "expdate are :"expdate
}else{
print "The are no expdate"
}
}'
  #3 (permalink)  
Old 04-10-2006
nymus7 nymus7 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 47
Unfortunately it doesn't run!!!
is the sign ~ not equal to = ???

-nm
  #4 (permalink)  
Old 04-10-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
$ echo "Hai" | nawk '
>BEGIN {FS=": "
getline expdate <"ExpDate.txt";
}

{
print "expdate :"expdate>print "expdate :"expdate
if (expdate ~ "[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]")
{
print "expdate are :"expdate
}else{
print "The are no expdate"
}
}'
>getline expdate <"ExpDate.txt";
>}
>{
>print "expdate :"expdate
>if (expdate ~ "[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]")
>{
>print "expdate are :"expdate
>}else{
>print "The are no expdate"
>}
>}'
expdate :10/04/2000
expdate are :10/04/2000

I runs perfectly, make sure you have copied correctly.
  #5 (permalink)  
Old 04-10-2006
nymus7 nymus7 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 47
Yeah, you're right! your program is running but not my script
Sometimes when you're concentrate on your problem you don't see other things! so, that's only a part of my program, here's a more "complete" part:

Code:
   ...
   ... |awk '
    BEGIN {FS=":  " 
    getline expdate <"ExpDate.txt"
    }
    $1 ~ /media ID/ {print "Media Number :\t"$NF}
    $1 ~ /first mount/ {print "first mount :\t"$NF}
    $1 ~ /number of mounts/ { 
      print "number of mounts :\t"$NF
      print "expdate1 :"expdate
      if (expdate ~ "[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]")
      { 
        print "expdate are :"expdate
      }else{
        print "The are no expdate"
      }
    }'

So, my question is : Is it possible to make an if-conditional in a search pattern ??? and I'll use the variable $NF (number of mounts) and expdate in the if-conditional ???
$1 ~ /<pattern>/ { if(conditional){<program>} }

Yeah, my explanation is perhaps a bit complicated! hope your understand!!
-nm
  #6 (permalink)  
Old 04-10-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
I would always say use nawk instead of awk... it works fine with nawk
Also i don't think you need to use $ symbol to refer the variable.

$ echo "first mount" | nawk '
> BEGIN { FS=": "
> getline expdate <"ExpDate.txt"
> }
> $1 ~ /media ID/ { print "Media Number :\t"NF }
> $1 ~ /first mount/ { print "first mount :\t"NF }
> $1 ~ /number of mounts/ {
> print "number of mounts :\t"NF
> print "expdate1 :"expdate
> if (expdate ~ "[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]")
> {
> print "expdate are :"expdate
> }else{
> print "The are no expdate"
> }
> }'
first mount : 1

It works fine.... I get the output
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0