HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- AWK and functions
Operating System - HP-UX
1829456
Members
1831
Online
109992
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
12-29-2003 03:37 AM
12-29-2003 03:37 AM
Hello ,
I am trying to "beautify" my awk script, and to do that, use the ability to create user functions within the awk script.
But unfortunately, my brain seems to have set "STUPID_CODE_WRITER" bit to TRUE and hence, I can't get the code to work.
I have written a test script called "test" (for obvious reasons, to run this script, type ./test !).
This is the code :
#!/bin/ksh
echo "Hello" | awk '{
greetstring = Hello($0);
printf(greetstring);
function Hello(hellostring) {
returnstring = sprintf ("%s from DJ" , hellostring);
return returnstring
}
}'
and this is what HP-UX11 says:
hp21:/home/tso/djh/Tardis [djh] > ./test
syntax error The source line is 5.
The error context is
>>> function <<< hello(hellostring) {
awk: The statement cannot be correctly parsed.
The source line is 5.
awk: A return statement must be inside a function.
The source line is 7.
hp21:/home/tso/djh/Tardis [djh] >
If anyone has a clue as to what I am doing wrong, you help is greatly appreciated.
Thanks in advance, and have a splendid 2004!
Cheers,
DJ
I am trying to "beautify" my awk script, and to do that, use the ability to create user functions within the awk script.
But unfortunately, my brain seems to have set "STUPID_CODE_WRITER" bit to TRUE and hence, I can't get the code to work.
I have written a test script called "test" (for obvious reasons, to run this script, type ./test !).
This is the code :
#!/bin/ksh
echo "Hello" | awk '{
greetstring = Hello($0);
printf(greetstring);
function Hello(hellostring) {
returnstring = sprintf ("%s from DJ" , hellostring);
return returnstring
}
}'
and this is what HP-UX11 says:
hp21:/home/tso/djh/Tardis [djh] > ./test
syntax error The source line is 5.
The error context is
>>> function <<< hello(hellostring) {
awk: The statement cannot be correctly parsed.
The source line is 5.
awk: A return statement must be inside a function.
The source line is 7.
hp21:/home/tso/djh/Tardis [djh] >
If anyone has a clue as to what I am doing wrong, you help is greatly appreciated.
Thanks in advance, and have a splendid 2004!
Cheers,
DJ
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2003 04:15 AM
12-29-2003 04:15 AM
Solution
a function definiton can be placed anywhere in a script that a pattern-action rule appears. Typically, we put the function definitions at the top of the script before the patter-action rules.
echo "Hello" |
awk '
function Hello(hellostring) {
returnstring = sprintf ("%s from DJ" , hellostring);
return returnstring
}
{
greetstring = Hello($0);
printf(greetstring); # do you really want no format specifier here
}'
echo "Hello" |
awk '
function Hello(hellostring) {
returnstring = sprintf ("%s from DJ" , hellostring);
return returnstring
}
{
greetstring = Hello($0);
printf(greetstring); # do you really want no format specifier here
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2003 08:35 PM
12-29-2003 08:35 PM
Re: AWK and functions
Thanks Curt!
The error I made was to put the function in the "body" of the awk statement, and it supposed to be declared before we arrive there.
It all works perfectly now, and I like to thank you for spending your time to help me out.
Cheers,
DJ
The error I made was to put the function in the "body" of the awk statement, and it supposed to be declared before we arrive there.
It all works perfectly now, and I like to thank you for spending your time to help me out.
Cheers,
DJ
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP