- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- about getline in awk
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
Forums
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
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
тАО05-27-2003 12:28 AM
тАО05-27-2003 12:28 AM
I'm trying to insert the string hour:minute:second at the begin of each line of a command output:
function ins
{
awk '{ "date +%H:%M:%S" | getline D; print D" "$0 }'
return 0
}
integer I=0
while (( I<5 ))
do
print $I
sleep 1
I=I+1
done | ins
but the output is:
10:25:30 0
10:25:30 1
10:25:30 2
10:25:30 3
10:25:30 4
It looks like the awk line "date +%H:%M%S | getline D" only is executed one time.
I'm a mess.
what is wrong ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 12:34 AM
тАО05-27-2003 12:34 AM
Re: about getline in awk
i think the line is correct, it is you that execute it only once !
Try this
integer I=0
while (( I<5 ))
do
D=$(ins)
print $I
sleep 1
I=I+1
done
So it should be execute each time you run the script, and not only once.
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 12:34 AM
тАО05-27-2003 12:34 AM
Re: about getline in awk
i think the line is correct, it is you that execute it only once !
Try this
integer I=0
while (( I<5 ))
do
D=$(ins)
print $D $I
sleep 1
I=I+1
done
So it should be execute each time you run the script, and not only once.
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 12:46 AM
тАО05-27-2003 12:46 AM
Re: about getline in awk
According to awk man pages, the awk action
{ "date +%H:%M:%S" | getline D; print D" "$0 }
is executed on every input line not on every awk execution, isn't it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 12:53 AM
тАО05-27-2003 12:53 AM
Re: about getline in awk
date +%H:%M:%S
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:00 AM
тАО05-27-2003 01:00 AM
Re: about getline in awk
yes, you are right.
Sorry I made a mistake, It seemed to me your line was
" done < ins "
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:14 AM
тАО05-27-2003 01:14 AM
Re: about getline in awk
You need to close the pipe, since awk cannot open more than one at a time:
function ins
{
awk '{ "date +%H:%M:%S" | getline D;close "date +%H:%M:%S"; print D" "$0 }'
return 0
}
integer I=0
while (( I<5 ))
do
print $I
sleep 1
I=I+1
done | ins
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:17 AM
тАО05-27-2003 01:17 AM
Re: about getline in awk
I thank all the others for the answers, today i learnt something new about awk !
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:26 AM
тАО05-27-2003 01:26 AM
Re: about getline in awk
I also ran the same script on a Solaris 8 system (/usr/xpg4/bin/awk is used instead of SunOs awk). The output is:
10:21:54 0
1
2
3
4
5
6
7
8
9
I suspect 'date' is executed just one time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:38 AM
тАО05-27-2003 01:38 AM
Re: about getline in awk
I had almost the same answer as 2 previous, and it works for me ...
Regards.
hp:/tmp> cat a
function ins
{
awk 'BEGIN {A= "date +%H:%M:%S"} {A | getline D ; print D" "$0 ; close(A) }'
return 0
}
integer I=0
while (( I<5 ))
do
print $I
sleep 1
I=I+1
done | ins
hp:/tmp> sh a
11:37:19 0
11:37:20 1
11:37:21 2
11:37:22 3
11:37:23 4
hp:/tmp>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 01:47 AM
тАО05-27-2003 01:47 AM
Re: about getline in awk
My version works with nawk, you'll need brackets on the close statement with "standard" awk.
I tried Dietmar's on my Solaris 8 system with /usr/xpg4/bin/awk, and it worked fine.
$ uname -a
SunOS schi0039pmh 5.8 Generic_108528-14 sun4u sparc SUNW,Ultra-80
$ cat script
function ins
{
/usr/xpg4/bin/awk '{ "date +%H:%M:%S" | getline D;print D" "$0 ;close("date +%H:%M:%S")}'
return 0
}
integer I=0
while (( I<5 ))
do
print $I
sleep 1
I=I+1
done | ins
$ script
04:46:50 0
04:46:51 1
04:46:52 2
04:46:53 3
04:46:54 4
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 02:00 AM
тАО05-27-2003 02:00 AM
Re: about getline in awk
Now it works fine.
Thanx everybody.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2003 02:01 AM
тАО05-27-2003 02:01 AM
Re: about getline in awk
Best regards...
Dietmar.