please see my requirement, I hope I am clear.
awk -v L=$( wc -l < filename ) 'NR==1{
header_p=sprintf "%s", substr($0,1,2);
header=sprintf "%s", substr($0,3,22);
} NR==L {
trailer_p=sprintf "%s", substr($0,1,2);
trailer=sprintf "%s", substr($0,3,22);
trailer_L=sprintf "%s", substr($0,25,8);
TL=sprintf "%08i", (L-2);
if((header_p=="HH") && (trailer_p=="TT") && (header==trailer) && (trailer_L==TL))
print "Validation Successful."
else
print "Validation Failed."
} ' filename
when I executed code given by you in debug mode it mentioned below error
I replace file name with $1 in script.
+ wc -l
+ 0< toa_mica_usage_feed00009554
+ awk -v L= 565568 NR==1{
header_p=sprintf "%s", substr($0,1,2);
header=sprintf "%s", substr($0,3,22);
} NR==L {
trailer_p=sprintf "%s", substr($0,1,2);
trailer=sprintf "%s", substr($0,3,22);
trailer_L=sprintf "%s", substr($0,25,8);
TL=sprintf "%08i", (L-2);
if((header_p=="HH") && (trailer_p=="TT") && (header==trailer) && (trailer_L==TL))
print "Validation Successful."
else
print "Validation Failed."
} toa_mica_usage_feed00009554
awk: syntax error near line 1
awk: bailing out near line 1
Use nawk
instead if your OS is SunOS or Solaris
Copy and use below code, pass filename as argument:
#!/bin/ksh
line_no=$( wc -l < $1 | sed 's/ //g' )
nawk -v L=$line_no 'NR==1{
header_p=sprintf("%s", substr($0,1,2));
header=sprintf("%s", substr($0,3,22));
} NR==L {
trailer_p=sprintf("%s", substr($0,1,2));
trailer=sprintf("%s", substr($0,3,22));
trailer_L=sprintf("%s", substr($0,25,8));
TL=sprintf("%08i", (L-2));
if((header_p=="HH") && (trailer_p=="TT") && (header==trailer) && (trailer_L==TL))
print "Validation Successful."
else
print "Validation Failed."
} ' $1
Below is the error after using nawk
nawk: can't open file NR==1{
header_p=sprintf "%s", substr($0,1,2);
header=sprintf "%s", substr($0,3,22);
} NR==L {
trailer_p=sprintf "%s", substr($0,1,2);
trailer=sprintf "%s", substr($0,3,22);
trai
source line number 1
I see you have used a different code, copy and use the nawk
code which I posted above.
---------- Post updated at 08:51 AM ---------- Previous update was at 08:42 AM ----------
Ok.thanks I lot, but I you can explain me how this code is doing it. for my understanding
Use a for
loop if you want to work on several files. Here is explanation of code.
#!/bin/ksh
for file in toa_mica_usage_feed*
do
line_no=$( wc -l < $file | sed 's/ //g' ) # Get line count of file
nawk -v L=$line_no 'NR==1{
header_p=sprintf("%s", substr($0,1,2)); # Extract 1-2 chars of header (HH)
header=sprintf("%s", substr($0,3,22)); # Extract 3-25 chars of header
} NR==L {
trailer_p=sprintf("%s", substr($0,1,2)); # Extract 1-2 chars of trailer (TT)
trailer=sprintf("%s", substr($0,3,22)); # Extract 3-25 chars of trailer
trailer_L=sprintf("%s", substr($0,25,8)); # Extract 25-32 chars of trailer (record length)
TL=sprintf("%08i", (L-2));
if((header_p=="HH") && (trailer_p=="TT") && (header==trailer) && (trailer_L==TL)) # Checking header and trailer and record length
print FILENAME " - validation Successful." # Print FILENAME and status
else
print FILENAME " - validation Failed." # Print FILENAME and status
} ' $file >> validation_result.txt # Redirect result to file: validation_result.txt
done
1 Like
ok working
Many thanks. it working fine