Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In an AWK script I am using a command

   system(date)

to print the current date in a file but after this command is executed next line is also added implicitly. Is there any way in AWK to print the current date without the new line being added so that whatever I print next comes in the same line?

share|improve this question

1 Answer 1

The date command adds a newline at the end of its output. You can read the output into awk and make it print without appending a newline.

"date" | getline date; printf "%s", date

Unless your script is long-running, you could alternatively obtain the date when you start your script.

awk -v date="$(date)" '
    … printf "%s", date …
'
share|improve this answer
    
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- –  nishan May 6 '11 at 19:36
    
@Gilles-every thing is working fine but it gives an error /bin/sh: 1 :not found .I am using it like this:- system("date"| getline date) printf("%s",date) –  nishan May 6 '11 at 19:44
1  
@nishan: The pipe executes its left-hand side as a command. So you're executing the result of system(…) as a shell command. The right syntax is in my answer. –  Gilles May 6 '11 at 20:00
    
@Gilles-thanks a lot –  Sushant Jain May 8 '11 at 5:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.