Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If pattern is not available, print a mesg 1

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
0
0
ZA
Hi All,

The problem that I am experiencing is when the pattern is not found in file2,
print a mesg "0133333333 No Info Available". I have tried different ways but cannot
seem to find a solution.

My Code
awk 'BEGIN{printf "%10s %25s %20s %15s\n","A_NUM","DATE/TIME", "B_NUM","DURATION"}
FNR==NR {
t[$1]=$1; next
}
{if(t[$1]==$1){
if($4=="0" && ($4!="[A-Z,a-z] || $4!=\"---")){
DUR=$4
}
else
{
DUR=substr($4,1,(length($4)-1))
}
print "%10s %20s %5s %20s %10s\n",$1, substr($2,1,10), substr($2,11,18), $3, (DUR)
}
}' file1 file2


file1
======
0111111111 2006/07/01
0122222222 2006/07/01
0133333333 2006/07/01

file2
=====
A_NUM disconnect_date_time B_NUM C_DUR
---------------- ---------------------------------------------- ------------------------ -----------
0111111111 2006/07/0310:01:21 0800000000 0
0111111111 2006/07/0310:12:50 0800000000 4930

(2 rows affected)
0122222222 2006/07/0911:20:53 66666666 1210
0122222222 2006/07/0922:03:42 0333333333 5890

(2 rows affected)
A_NUM disconnect_date_time B_NUM C_DUR
---------------- ---------------------------------------------- ------------------------ -----------

(0 rows affected)


expected results
==============

A_NUM DATE/TIME B_NUM DURATION
0111111111 2006/07/03 10:01:21 0800000000 0
0111111111 2006/07/03 10:12:50 0800000000 493

0122222222 2006/07/09 11:20:53 66666666 121
0122222222 2006/07/09 22:03:42 0333333333 589

0133333333 No Info Available

Many Thanks
Chris
 
Hi

[ul]
[li]Initialize a counter with 0.[/li]
[li]Each time when a record is found/processed/printed increment the counter.[/li]
[li]At the end check the counter and if is still 0 print the message.[/li]
[/ul]

Feherke.
 
Hi Feherke,

Thanks, but I have tried it

awk 'BEGIN{printf "%10s %25s %20s %15s\n","A_NUM","DATE/TIME", "B_NUM","DURATION"}
FNR==NR {
t[$1]=$1; next
}
{if(t[$1]==$1){Flag=1}{
if($4=="0" && ($4!="[A-Z,a-z] || $4!=\"---")){
DUR=$4
}
else
{
DUR=substr($4,1,(length($4)-1))
}
if(Flag==1){
print "%10s %20s %5s %20s %10s\n",$1, substr($2,1,10), substr($2,11,18), $3, (DUR)
}
else
{
print "Number Not Avai"
}
}' file1 file2


Result
======
A_NUM DATE/TIME B_NUM DURATION
Number Not Avai
Number Not Avai
0111111111 2006/07/03 10:01:21 0800000000 0
0111111111 2006/07/03 10:12:50 0800000000 493
(2 rows affected)
0122222222 2006/07/09 11:20:53 66666666 121
0122222222 2006/07/09 22:03:42 0333333333 589

(2 rows affected)
A_NUM disconnect _date_time B_NUM C_DU
---------------- ---------- ------------------ ------------------------ ----------

(0 rows affected)

Maybe you can guide me to what I am doing wrong.

Thanks Again
Chris

 
Hi

[ol]
[li]Do not put the Flag setting and the proper operation in separate block. The [tt]if[/tt] will affect only the first.[/li]
[li]Put the flag testing at the end, so in the [tt]END[/tt] block.[/li]
[/ol]
Code:
awk 'BEGIN{printf "%10s %25s %20s %15s\n","A_NUM","DATE/TIME", "B_NUM","DURATION"}
        FNR==NR {
        t[$1]=$1; next
        }
        {if(t[$1]==$1){Flag=1 [gray]# }{ <- what the ...[/gray]
                if($4=="0" && ($4!="[A-Z,a-z] || $4!=\"---")){
                    DUR=$4
            }
            else
            {
                DUR=substr($4,1,(length($4)-1))
            }
       } 
[red]END {[/red]
        if(Flag==1){
        print "%10s %20s %5s %20s %10s\n",$1, substr($2,1,10), substr($2,11,18), $3, (DUR)
        }
        else
        {
        print "Number Not Avai"
        }
       [red]}[/red]' file1 file2

Feherke.
 
Hi Feherke,

Thanks a lot. With you code I get the following error

syntax error The source line is 14.
The error context is
>>> END <<< {
awk: The statement cannot be correctly parsed.


CODE
====
awk 'BEGIN{printf "%10s %25s %20s %15s\n","A_NUM","DATE/TIME", "B_NUM","DURATION"}
FNR==NR {
t[$1]=$1; next
}
{if(t[$1]==$1){Flag=1
if($4=="0" && ($4!="[A-Z,a-z] || $4!=\"---")){
DUR=$4
}
else
{
DUR=substr($4,1,(length($4)-1))
}
}
END {
if(Flag==1){
printf "%10s %20s %5s %20s %10s\n",$1, substr($2,1,10), substr($2,11,18), $3, (DUR)
}
else
{
print "Number Not Avai"
}
}
}' file1 file2

Many Thanks
Chris
 
Hi

Sorry, your styleless indenting confused me. Needs another brace ( } ) before the [tt]END[/tt].
Code:
awk '
BEGIN {
  printf "%10s %25s %20s %15s\n","A_NUM","DATE/TIME", "B_NUM","DURATION"
}

FNR==NR {
  t[$1]=$1; next
}

{
  if (t[$1]==$1) {
    Flag=1
    if ($4=="0" && ($4!="[A-Z,a-z] || $4!=\"---")) {
      DUR=$4
    } else {
      DUR=substr($4,1,(length($4)-1))
    }
  }
}
END {
  if (Flag==1) {
    print "%10s %20s %5s %20s %10s\n",$1, substr($2,1,10), substr($2,11,18), $3, (DUR)
  } else {
    print "Number Not Avai"
  }
}
' file1 file2

Feherke.
 
Hi

Thanks once again. The result i am getting is incorrect


A_NUM DATE/TIME B_NUM DURATION
(0 rows affected)

Code:
        awk 'BEGIN {printf "%10s %25s %20s %15s\n","A_NUM","DATE/TIME", "B_NUM","DURATION"
                        }
          FNR==NR {
           t[$1]=$1; next 
          }  
            
        {if (t[$1]==$1) {       
                        Flag=1
                        if ($4=="0" && ($4!="[A-Z,a-z] || $4!=\"---")) {
                                DUR=$4
                        }
                        else
                        {
                                DUR=substr($4,1,(length($4)-1))      
                        }
                }        
        }
                END{
                        if (Flag==1) {
                        printf "%10s %20s %5s %20s %10s\n",$1, substr($2,1,10), substr($2,11,18), $3, (DUR)                         
                        }
                        else
                        {
                                print "Number Not Avai"                        
                        }
                        }' $1 $2
Any idea

Thanks
Chris
 
Hi PHV,Feherke,

Any ideas for me.

Many Thanks
Chris
 
You wanted something like this ?
Code:
awk '
BEGIN {
  fmt="%10s %25s %20s %15s\n"
  printf fmt,"A_NUM","DATE/TIME","B_NUM","DURATION"
}
FNR==NR {
  t[$1]=$1;f[$1]=0;next
}
$1 in t {
  ++f[$1];printf fmt,$1,$2,$3,$4
}
END {
  for(i in f) if(!f[i])print i" No Info Available"
}
' file1 file2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

As always spot on. One question , may I borrow your brain for one year, I promise to return it in the same order.(just a joke)

Many Thanks
Chris
 
Code:
function say( a, b, c, d )
{ printf "%10s %25s %20s %15s\n", a, b, c, d }

BEGIN { say( "A_NUM","DATE/TIME", "B_NUM","DURATION" ) }

ARGV[1] == FILENAME { a[$1] }

ARGV[2] == FILENAME  &&  $1 in a {
  b[$1]
  say( $1, $2, $3, $4 ) }

END {
  for (key in a)
    if ( ! (key in b) )
      print key, "No Info Available" }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top