![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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" } }' |
|
||||
|
$ 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. |
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|