Please help!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help!!!
# 1  
Old 07-19-2006
Please help!!!

All,

I need l help in writing a script;

I have a file with header on the first row data rows next and the last row in the file contains the record count of the data records only. I need to remove the last and first line and check to see that the data rows match the count on the last line. Can any one plzz help.

for eg file looks like this;

market|code|recordno|usage
a|2|3|4o
b|6|7|80
2

I need to remove the header and last row and count if the data rows equals the count at the bottom. Here data rows are 2 and the count is 2 hence good.
If not i need to through an error "data rows don't match count. Please help.
# 2  
Old 07-19-2006
Code:
awk 'END{ printf ($1+2 != NR ) ? "Error\n" : "" }' file

# 3  
Old 07-19-2006
Reborg,


Thanks very much for the code!! But after checking whether the record count matches i need to remove the first record and last record from the file and write the data rows to a new file. How can I acheive this? Please help.
# 4  
Old 07-19-2006
Code:
awk '{data[NR] = $0} END { if ( NR - 2 == $1) { for ( i=2 ; i < NR; i++ ) { print data[i]}}else { print "error"} }' file > newfile

# 5  
Old 07-19-2006
Reborg,

Thank you very much for your great response. I am new to use awk and really it's a great feeling learning what it can do on files. I have one more question. If i need to do this on multiple files and write multiple output files as said in the earlier reply is it possible?

If I have 20 files with the same format in a directory can I apply the same code on all of them at one time and produce 20 output files if the data rows in each file matches the count in them. How can the same code read multiple files that start with similar pattern like for e.g REG1, REG2,REG3 etcc... Kindly let me know if that is possible or I have to run independently.

Once again thank you very much.
# 6  
Old 07-19-2006
one idea - based on the one from reborg:
Code:
awk '{data[NR] = $0; out= FILENAME "new"; file[NR]=out;} END { if ( NR - 2 == $1) { for ( i=2 ; i < NR; i++ ) { print data[i]} > file[i]; close(file[i]}else { print "error"} }' REG*

# 7  
Old 07-20-2006
Hi Vgersh,

I am getting an error like this

awk '{data[NR] = $0; out= FILENAME "new"; file[NR]=out;} END { if ( NR -
2 == $1) { for ( i=2 ; i < NR; i++ ) { print data[i]} > file[i];
close(file[i]}else { print "error"} }' arun* syntax error The source
line is 1.
The error context is
{data[NR] = $0; out= FILENAME "new"; file[NR]=out;} END
{ if ( NR - 2 == $1) { for ( i=2 ; i < NR; i++ ) { print data[i]} >>> >
<<< file[i]; close(file[i]}else { print "error"} }
awk: The statement cannot be correctly parsed.
The source line is 1.
awk: There is a missing ) character
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question