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!

Manipulating Last Line In Input File

Status
Not open for further replies.

mhodges88

Programmer
Apr 1, 2001
5
US
I am new to AWK and I have what seems to be an easy task, but am having trouble manipulating the last line in the file. I figured out how to print the last line of the file to a new file by END{print > filename} because it stores that last line in a buffer, but let's say I want to print everything in the file to another file, with the exception of that final line to a different file, but I don't have a pattern to search for as this line is always going to be variable. Is there a simple way to do this? Thanks.

Mark
 
Something like this ?
awk '
NR>1{print a > "/path/to/file1"}
{a=$0}
END{print a > "/path/to/file2"}
' /path/to/inputfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Unfortunately that doesn't work for me. That omits the first record from being printed to file1, but I need the last record to be omitted.

Mark
 
I'm atonished as I've tested the script before posting, and one more time now.
Can you please post the EXACT sequence you use ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sorry PH...it was late last night and I should've gone into more detail on what I was trying to accomplish! I really appreciate you helping me out. Here's my scenario:

I will have 1 input file that will have a variable number of records....more than likely thousands of records daily...FS=\n...I need to redirect all records in the input file (except the very last record) into one file, and the very last line into another. I figured out how to get the last line into a seperate file, but I'm having trouble getting everything but the last line into the first file. Since the very last line of the file will be variable, I have no pattern to match on. Or another way I could get around this would be to delete the last line of the input file and use that as my first file...but I can't delete it in the main program because I need to redirect it in the END statement. By the way, I'm on windows. If I was on Unix this would be really easy. Thanks.

Mark
 
And what is the problem with my awk script ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hey PH...you are correct...that does work...I had to change the brackets around a bit to get it to work in my awk script like this: However, it's putting a blank line at the top of filename1. That sounds easy to correct...thank you so much for your tip. I knew there had to be a way to do that.

{
{NR > 1} print a > "filename1"
{a=$0}
}

END{print a > "filename2"}

Mark
 
This is ABSOLUTELY not the same script as mine, I'm sorry.
If you awk version accept something like -f scriptname.awk as arguments, then create an awk script with this lines:
NR > 1 { print a > "filename1" }
{ a = $0 }
END { print a > "filename2" }

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
That works great PH....I think I had an extra bracket somewhere that was giving me all kinds of parse errors when I tried it the first time....I really appreciate it. How long have you been programming with AWK?

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top