LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-08-2016, 09:10 AM   #1
lazer00
LQ Newbie
 
Registered: Mar 2011
Posts: 16

Rep: Reputation: 0
Print smallest integer from file using awk custom function?


`awk` function looks like this in a file name `fun.awk`:

Quote:
{
print small()
}
function small()

{
a[NR]=$0

smal=0

for(i=1;i<=3;i++)
{
if( a[i]<a[i+1])

smal=a[i]

else

smal=a[i+1]

}
return smal
}
The contents of `awk.write`:

Quote:
1
23
32
The `awk` command is:

awk -f fun.awk awk.write

It gives me no result? Why?
 
Old 01-08-2016, 10:04 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Use a simpler program, e.g.
Code:
NR==1 || $1 < minimum { minimum = $1 }
END { print minimum }
This assumes that the first field of each record is the number we check, and that all numbers are 0 or positive. It's not tested.
 
1 members found this post helpful.
Old 01-08-2016, 12:05 PM   #3
lazer00
LQ Newbie
 
Registered: Mar 2011
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by berndbausch View Post
Use a simpler program, e.g.
Code:
NR==1 || $1 < minimum { minimum = $1 }
END { print minimum }
This assumes that the first field of each record is the number we check, and that all numbers are 0 or positive. It's not tested.
thanks for the one liner but can this code be translated to a function?
 
Old 01-08-2016, 06:27 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
It would be rather different, clumsy and un-awk-ish. Why do you insist on a function?

And what is that function's specification - input and output?

Let me elaborate why I think a function is the wrong solution. awk programs consist of pattern-action pairs which work on one line at a time. Users can define functions for better structuring their actions.
Your task requires processing the entire file. While that is possible inside a single awk action, for instance using the built-in function getline, it's against awk's nature. It will also be hard to read compared to my two-line program.

Last edited by berndbausch; 01-08-2016 at 06:41 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print the file names using AWK Newman1 Programming 5 07-26-2015 06:04 AM
[SOLVED] awk: cmd. line:1: fatal: cannot open file `{print' for reading (No such file or direc linuxandtsm Programming 9 10-28-2013 09:13 AM
[SOLVED] Bash/awk extract function from file. apottere Linux - Newbie 38 08-03-2013 06:45 PM
[SOLVED] using awk to print log in stats to a file demet8 Linux - Newbie 4 10-17-2012 09:42 PM
[SOLVED] awk print to file question takayama Programming 3 02-05-2011 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration