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

shell script or awk for automated fields checking ? need help

Status
Not open for further replies.

chz013

Programmer
Jan 5, 2003
73
0
0
US
I have a record that is like this below; records like this
are delimited by --- lines; I need to write a bash script
that automatically checks for almost all the fields
defined on each row;
1- Is it difficult to write a shell script that processes
these records and checks each field in each record ?
Do you have an example script that reads a file that
contain all these records and make checks on them ?
2 - Is it better to write an awk script over shell script
if I only need to check about 10 - 12 fields in each record ? Do you have an example script that reads a file
that contain all these records and make checks on them ?

Any comments or help is appreciated.
Advance thanks

----------------------------------------------------------
Record Checksum: 110
Record Number: 1
Reference Number: 1
Record Type: 2 => MTC

IM Field Content:
IM field: (001) => IM
EVEN/ODD bit: (1) => odd
IM Id: 316

SType: 0x00 => LS
CNumber Field Content:
L: 1
Ext: (1) => No
Tnum: (001) => No
PId: (0001) => No
D:

ONum Field Content:
L: 6
Ext: (1) => No
Tnum: (001) => No
PId: (0001) => No
D: 1234567890

TNum Field Content:
L: 6
Ext: (1) => No
Tnum: (001) => No
PId: (0001) => No
D: 0987654321

CTime: 0x0000 =>
S Du: 0
C Du: 0x1 => 1
L ID: 5
C Id: 0
Cause for T: 1 => N Re
O.t g ID: 0

Diag hex dump:
00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
----------------------------------------------------------
 
What have you so far ?
Basically, awk seems to be your friend.
Do a
Code:
 man awk
and take a look to associative array and the -F option.
Note:Cross-posting (thread822-642242) is an unfair practice.

Hope This Help
PH.
 
If your fields are in a fixed order then perhaps you could load a numeric array and then do validation on each.

awk 'BEGIN {FS=":"}
/Record Checksum:/ {
#----load all fields in to array a
for (i=0; i<42; i++) {
getline;
a=$2;
}
#---test &quot;IM Id&quot; field (7th in array)
if ( a[7] !~ &quot;316&quot; ) {
print &quot;Error in IM Id! Record number: &quot; a[0];
}
#---test &quot;TNum PId&quot; field (28th in array)
if ( a[28] !~ &quot;0001&quot; ) {
print &quot;Error in TNum PId! Record number: &quot; a[0];
}
}' file1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top