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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read records and values in a file? 2

Status
Not open for further replies.

zanzemaj

Programmer
Nov 2, 2006
35
NZ
I need help on a script I'm doing. I need to read a textfile named fexit.txt with sample records:
Code:
job     excode
1       0
2       8
3       0
4       12

obj    typ    sze
temp   tab    8,854,555
test   tab    12,222,840,111

I need to check if all excode is equal to 0.
If not all excode is 0 then go to sleep for 60 seconds and go through the file again until ALL excode is equal to 0. If not all excode is equal to 0, then error message.

Need your help. I know I need to use grep. Your help is greatly appreciated.

zanzemaj
 
And what have you tried so far ?
I know I need to use grep
you may also consider awk.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Please feel free to suggest another approach if needed
Code:
grep "0" fexit.txt > dummyfile0.txt
grep "8" fexit.txt > dummyfile8.txt
grep "12" fexit.txt > dummyfile12.txt

if [ -e dummyfile8.txt || -e dummyfile12.txt ];then
  echo "Error"
else
  sleep 60
  not sure how to proceed
fi
 
Hi

Code:
while :; do test "$( sed -n '2,/^$/{/\s0$/! p}' /input/file )" || break  && { echo "error : non-zero exit codes"; sleep 6; }; done
Tested with GNU [tt]sed[/tt].

Feherke.
 
A more legacy approach (;-):
while :;do awk '!NF{exit 0}NR>1 && NF==2 && $2{exit 1}' fexit.txt && break;echo "ERROR";sleep 60;done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you feherke and PHV. I tried feherke's code but it only checks the first row of the file? How do I go through all the contents of the file. It has a dynamic number of lines.
 
My awk suggestion goes from line 2 until empty line or excode<>0
Have you tried it ?
 
Thank you feherke, but I can't seem to make it work as it is intended.

Hi PHV, I tried it as:
Code:
while :;do awk '!NF{exit 0}NR>1 && NF==2 && $2{exit 1}' fexit.txt  && break;echo "ERROR";sleep 60;done
But the sample records do not have all zeros, I should get an error. However, I am not getting any error in testing this line.

Thank you all for your response, really appreciate them.

 
Hi

Are you sure the fexit.txt file sample you gave us is correct ? Because PHV's solution works for me too and I see no weakness where it could fail.

What shell and [tt]awk[/tt] implementation are you using ?

Feherke.
 
Sorry for that, yes it works based on the sample fexit.txt. I'm using Korn Shell. I don't know about what awk implementation is being used, may I know how? Thanks a lot.

I'm so sorry, now the file has additional information and now looks like this. And I guess the code has to change. I tried but I cannot make it work.
Code:
Job info for Job ID '1234' using My view


Job "xxxx_multi_xxxx_4" - 2007-03-05 19:37:52
    (ID 1234)

Operation: XXXXXXX
    TDPID: xxxx
     Jobs: 4


Job     Client             Size      Time Excode
------  --------- ------------- --------- --------
1       1         3,052,057,083    :00:55        0
2       2         3,052,476,196    :00:54        8
3       1         3,051,850,142    :00:53        0
4       2         3,052,456,715    :00:52        8
 
You may try this:
Code:
while :; do 
  awk '
$5=="Excode"{r=NR+1}
r && NR>r && NF==5 && $5{exit 1}
' fexit.txt && break
  echo "ERROR"; sleep 60
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you PHV, your code works as expected. I have one more question, what if I want to check only twice? When I find a non-zero Excode, go to sleep 60 and then check again. After the second try, if I find a non-zero Excode again, then Error.
 
Typed, untested:
typeset -i i=3
while ((i-=1)); do
awk '
$5=="Excode"{r=NR+1}
r && NR>r && NF==5 && $5{exit 1}
' fexit.txt && break
sleep 60
done
((i)) || echo "ERROR"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That was fast, and it works. I also changed the code so that there will be sleep once only.
Code:
typeset -i i=2
while ((i-=1)); do
  awk '
$5=="Excode"{r=NR+1}
r && NR>r && NF==5 && $5{exit 1}
' fexit.txt && break
  sleep 60
done
((i)) || echo "ERROR"
Thank you so much, and I am learning a lot from you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top