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!

How to read data file name and write function in a awk script?

Status
Not open for further replies.

goyalpdm

Programmer
Jan 11, 2003
42
US
Hi,

I have a awk script (awk_script.awk) which takes data file (data_file - pipe seperated data) as input and generates another file in a particular format...

Here is the command i use -
nawk -f awk_script.awk data_file

I need to -
1. Know the name of data_file inside my awk_script.awk
2. Write a small function in awk_script.awk to which I can pass some arguments....

Please see if you could help... ?
Thanks,
Sachin
 
1. Know the name of data_file inside my awk_script.awk

print FILENAME;

2. Write a small function in awk_script.awk to which I can pass some arguments....

function myAwkFunction(myArg) {

} vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Functions can appear anywhere in a script and are preceded by keyword 'function'
eg
#!/bin/ksh
data_file_id=&quot;/dirname/datafile&quot;
# pass file name in
awk -v filein=&quot;$data_file_id&quot;'{
function fIsLeapYear(pYear)
{
return (!(pYear%400))-(!(pYear%100))+(!(pYear%4))
} # end of function

function fDaysIn(pMonth,pYear)
{
if(pMonth==2) return 28+fIsLeapYear(pYear)

if(pMonth==4 || pMonth==6 || pMonth==9 || pMonth==11) return 30

return 31
} # end of function

function fJulianToDate(pDGJulian)
{
gYear=1968;
gMonth=1

# Increment year
while( pDGJulian-(365+fIsLeapYear(gYear)) > 0 )
{
# Increment year
pDGJulian-=365+fIsLeapYear(gYear)
gYear++
}

# Increment month
while( pDGJulian-fDaysIn(gMonth,gYear) >0)
{
pDGJulian-=fDaysIn(gMonth,gYear)
gMonth++
}

# what is left is day of month
gDay=pDGJulian
gDate=sprintf(&quot;%02s/%02s/%02s&quot;, gDay,gMonth,gYear%100)
return gDate

} # end of function
#
#Pass a parameter to a function
newdate=fJulianToDate(12321)
print newdate
#just print the passed-in file name
print filein

} ' $data_file_id


HTH Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top