- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk - end of file from getline call
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2009 06:46 AM
тАО02-05-2009 06:46 AM
We are using a small awk script to capture any entry in file_a that is not in file_b.
Both files are sorted on the same order.
The program fails when the last entries of file_b are missing.
The call is: awk -f awk_script_name file_a
file_b is matched using commands:
getline <"file_b">0
LASTFS=$1
How can I make LASTFS=999999999 when I reach end-of-file on file_b?
Thanks all
RayB
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2009 10:25 AM
тАО02-05-2009 10:25 AM
Solutiongetline <"file_b">0
LASTFS=$1"
Not really. getline reads one line from "file_b". typically it would be used w/ and if test or a while loop, as in
if (getline <"file_b" > 0)
{do stuff}
or
while (getline<"file_b" >0) {
do stuff}
that (at least to me) implies that you are reading thru file_b once for every line in file_a. If so, thats not real efficient.
why write this in awk when grep will do it?:
grep -v -f file_b filea
or
comm filea fileb -2 -3???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2009 12:15 PM
тАО02-05-2009 12:15 PM
Re: awk - end of file from getline call
You solved my immediate problem: I will use "comm -23 file_a file_b" instead because it is faster than grep.
The original was written using awk so I tried to fix it first.
However, I would still like to know how to trap this getline EOF for future use.
I tried reading examining $?, $0, NF etc but it looks like those aren't reset when EOF is reached. I also tried using if within the do while loop but nothing has worked.
I'd like all that effort not to be wasted for nothing.
Here is the actual code:
BEGIN{
FS="." # remove the .doc and .DOC
LASTFS=0
}
{
# The database list is on std. input
db=$1
# print db, " from db"
if ( db < LASTFS ){
print db, "is missing" >>"missing_forms"
}
else{
if ( db == LASTFS ){
print db, " is ok" >>"present_forms"
}
else{
do {
getline <"files_numbers_only">0
# print $1, " read from file"
LASTFS=$1
}while(LASTFS
if( db == LASTFS ){
print db, " is ok" >>"present_forms"
}
else{
print db, " is missing">>"missing_forms"
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2009 12:37 PM
тАО02-05-2009 12:37 PM
Re: awk - end of file from getline call
getline <"files_numbers_only">0
# print $1, " read from file"
LASTFS=$1
use
a=getline <"files_numbers_only">0
if(a==1)
{
LASTFS=$1
}
else
{
LASTFS=999999999
}
The getline function returns a value indicating success or failure - just trap that returned value, then make your decisions based on the status of getline.
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2009 12:46 PM
тАО02-05-2009 12:46 PM
Re: awk - end of file from getline call
ok, so....
x=(getline < "yourfile") ;
sets "x" = return code of getline and $x to whatever got read from the file.
in looking at the code you posted, it looks like you are referring to this segment:
else
{
if ( db == LASTFS )
{
print db, " is ok" >>"present_forms"
}
else
{
do
{
getline <"files_numbers_only">0
# print $1, " read from file"
LASTFS=$1
}while(LASTFS
if( db == LASTFS )
this: "getline <"files_numbers_only">0",
I would think this needs to be either
if (getline<"file_numbers_only" >0)
{LASTFS = 9999999;
}
---or---
x=(getline<"file_numbers_only");
if ( x > 0) ........
note that this construct: getline <"files_numbers_only">0 is half of a logical test,
the 'getline <"files_numbers_only"' tells getline to redirect its input to the named file. The " > 0" should have been part of a logical test.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2009 12:54 PM
тАО02-05-2009 12:54 PM
Re: awk - end of file from getline call
Your solutions work fine.
I finaly found the reason why none of the changes I was attempting were making any difference:
I was executing the original script after every change I made to my work copy.
My forehead is very flat right noow!
Take care and Thanks again.
RayB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-06-2009 07:47 AM
тАО02-06-2009 07:47 AM