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

Parse And Save 1

Status
Not open for further replies.

devangelista

Technical User
May 14, 2002
12
US
Hello Awk Guru's I've searched the threads a bit and have some idea of how to do this but I can't seem to get it right.

Here's what I'm trying to do:

I receive files from the Federal Reserve Bank in the following format: AHMMDDXI.001 or AHMMDDXI.002

Inside these files are header records at positions 1 - 14, a sample header is: 101 083901825

I need to parse all files in a directory that have either .001 or .002 extensions, examine the file header and evaluate the "083901825" part and based on what it is save the file AHMMDDXI.001 or .002 as another file name, move the original source file to an archive location, and repeat this for every file in the directory.

For Example:
Input File: AHMMDDXI.001
If Header Rec: 101 083901825
Then Output File: AHMMDDXI.ALT
Else If Header Rec: 101 061308770
Then Output File: AHMMDDXI.FED
Move or archive: AHMMDDXI.001 to hard coded Location

Repeat for each file in the directory.

Any assistance would be greatly appreciated. We are despirate...

Best Regards

David Evangelista
FiNET, Inc.
 
I hope this is what you want. You'll have to change the archive directory to what you want. I wasn't too sure on the header specifics so this searches the entire file on the first 13 characters per line - a good point to optimize. Good luck.

ls | awk '
/\.00[12]/{
fn = $0
while ((getline < fn) > 0){
hrec = substr($0,1,13)
if( hrec == &quot;101 083901825&quot; )
system( &quot;cp &quot; fn &quot; &quot; substr(fn,1,length(fn)-4) &quot;.ALT&quot; )
else if( hrec == &quot;101 061308770&quot; )
system( &quot;cp &quot; fn &quot; &quot; substr(fn,1,length(fn)-4) &quot;.FED&quot; )
}
close(fn)
system( &quot;mv &quot; fn &quot; $HOME/storage&quot; )
}'
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
bigoldbulldog - Thank you for your post. It does not seem to be working.

I saved the script as frb.awk and when I run awk frb.awk
I receive the error:

ls | awk '
^
awk ERROR frb.awk line 1: syntax error

Not sure what this means. If I remove the line then
I receive the error:

}'
^
awk ERROR frb.awk line 12: syntax error.

It seems to be the ' at the end of the lines.

If I remove the ' mark from the code, it seems to just hang and I have to press Control-C to break out. Any ideas??

Thanks

Dave ///
 
My mistake. Can't run the ls command .. I'm on a MS-DOS system running the DOS awk.exe program.

I tried dir | awk -f frb.awk but get a bad command or file name error.

Thanks for setting me straight on the syntax vgersh99 :)

Dave ///
 
details........:)

make sure your &quot;awk.exe&quot; [or whatever awk executable is named] is on your &quot;Path&quot;. This becomes a system-related issue.

vlad
 
vlad,

Thanks -- Yeah -- pretty stupid of me to not include that I was not on unix. LOL

Actually, the AWK.EXE application is in my windows path but I get the bad command error still.

:)

Dave ///
 
I'm going to need some more help with this. I've found putting an x on the getline actually reads the first line, then referencing x actually retreives the header information. I've changed the routine a bit to hard code file names &quot;for now&quot; and this all works when there is only one file in the directory. Whenever there is more than 1 file in the directory it does not work. Additionally, the move command in previous posts does not work. I've downloaded a simulator to simulate the ls, cp, and mv commands so I'm getting somewheres.

Pleeezzz help... Thanks again. Dave ///

/\.00[12]/{
fn = $0
while ((getline x < fn) > 0){
hrec = substr(x,1,13)
if( hrec == &quot;101 083901825&quot; )
system( &quot;cp &quot; fn &quot;allot.txt&quot; )}
else if( hrec == &quot;101 061308770&quot; )
system( &quot;cp &quot; fn &quot;fedsys.txt&quot; )
}
close(fn)
}
 
Hi Dave-

I modified the script by bigoldbulldog and it seems to work. Try it and change things the way you need them.

I tried to set it up to allow some flexibility in how you want to move (mv) files, etc.



/\.00[12]/{
fn = $0
nf = substr(fn,1,length(fn)-4)
while ((getline x < fn) > 0){
hrec = substr(x,1,13)
if( hrec == &quot;101 083901825&quot; )
system( &quot;cp &quot;fn&quot; &quot;nf&quot;.alt&quot; )
# system( &quot;cp &quot;fn&quot; &quot;nf&quot;_&quot;++i&quot;.ALT&quot; ) # use &quot;_&quot;++i if input basename is not unique
if( hrec == &quot;101 061308770&quot; )
system( &quot;cp &quot;fn&quot; &quot;nf&quot;.fed&quot; )
# system( &quot;cp &quot;fn&quot; &quot;nf&quot;_&quot;++j&quot;.FED&quot; ) # use &quot;_&quot;++j if input basename is not unique
}

close(fn)

if (( hrec == &quot;101 083901825&quot; ) || ( hrec == &quot;101 061308770&quot; )) # if you want all
# input files moved even if they fail on header record; then, delete this if statement
system( &quot;mv &quot;fn&quot; ./test/&quot;fn&quot;&quot; )
}


Hope this helps do the job!


flogrr
flogr@yahoo.com

 
Special Thanks to everyone who participated in my requests. I have made a combination of the code outlined above and I have resolved the problem.

If it were not for the generous postings, I would have never completed this project!!

Thank you everyone for all your help...

David Evangelista
FiNET, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top