Print date at END clause of AWK

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Print date at END clause of AWK
# 1  
Old 11-06-2009
Print date at END clause of AWK

Hi to all! I 'm new in unix programing so... may be I decided a wrong tool to solve the problem but anyway... all road goes to rome jajaja.

My question is: There is any way to print date at the END clause of an AWK script. I mean, I'm writing a tool with AWK and the results are redirected to a file.
Some thing like:

BEGIN{
"date" |getline;
print >>TestLog.log
}

These lines works "sweetly" at the BEGIN clause... but it doesn't at the END clause.
I'm working on a HP-UX (unknow version Smilie)

I know that this is not actually a big challenge (jajaja) but I googled as much I could without results.

How could I log the end of the AWK script execution?

Thanks matrixmadhan for you support Smilie

F
# 2  
Old 11-06-2009
Use getline var instead of getline only.

Code:
$ awk < /dev/null 'BEGIN {
  if (!("date" | getline dt))
    print "Error getting date"
  }
END {
  print "date is:", dt
  }'
date is: Fri Nov  6 12:29:53 MET 2009

# 3  
Old 11-06-2009
Quote:
Originally Posted by radoulov
Use getline var instead of getline only.

Code:
$ awk < /dev/null 'BEGIN {
  if (!("date" | getline dt))
    print "Error getting date"
  }
END {
  print "date is:", dt
  }'
date is: Fri Nov  6 12:29:53 MET 2009

Hi dude, thanks for your reply!

Actually, works like you said, but you are storing the date at the beging of the script execution. I need the date/time when the script ends.

Thanks for your reply

F
# 4  
Old 11-06-2009
Quote:
Originally Posted by fmeriles
... but you are storing the date at the beging of the script execution. I need the date/time when the script ends.
[...]
You can use something like this:

Code:
awk <<< 1  'BEGIN {
  printf "start: "; system("date")
  }
system("sleep 3")
END {
  printf "end: "; system("date") 
  }'


Code:
zsh-4.3.10[sysadmin]% awk <<< 1  'BEGIN {
  printf "start: "; system("date")
  }
system("sleep 3")
END {
  printf "end: "; system("date")
  }'  
start: Fri Nov  6 15:38:53     2009
end: Fri Nov  6 15:38:56     2009



---------- Post updated at 03:47 PM ---------- Previous update was at 03:31 PM ----------

Actually, you just need to close the external command:

Code:
awk <<< 1  'BEGIN {
  "date" | getline dt
  close("date")
  print "start:", dt
  }
system("sleep 3")
END {
  "date" | getline dt
  close("date")
  print "end:", dt 
  }'


Like this:

Code:
zsh-4.3.10[sysadmin]% awk <<< 1  'BEGIN {
  "date" | getline dt
  close("date")
  print "start:", dt
  }
system("sleep 3")
END {
  "date" | getline dt
  close("date")
  print "end:", dt
  }' 
start: Fri Nov  6 15:47:23     2009
end: Fri Nov  6 15:47:27     2009


Last edited by radoulov; 11-06-2009 at 10:39 AM..
# 5  
Old 11-06-2009
Quote:
Originally Posted by radoulov
You can use something like this:

Code:
awk <<< 1  'BEGIN {
  printf "start: "; system("date")
  }
system("sleep 3")
END {
  printf "end: "; system("date") 
  }'


Code:
zsh-4.3.10[sysadmin]% awk <<< 1  'BEGIN {
  printf "start: "; system("date")
  }
system("sleep 3")
END {
  printf "end: "; system("date")
  }'  
start: Fri Nov  6 15:38:53     2009
end: Fri Nov  6 15:38:56     2009

---------- Post updated at 03:47 PM ---------- Previous update was at 03:31 PM ----------

Actually, you just need to close the external command:

Code:
awk <<< 1  'BEGIN {
  "date" | getline dt
  close("date")
  print "start:", dt
  }
system("sleep 3")
END {
  "date" | getline dt
  close("date")
  print "end:", dt 
  }'


Like this:

Code:
zsh-4.3.10[sysadmin]% awk <<< 1  'BEGIN {
  "date" | getline dt
  close("date")
  print "start:", dt
  }
system("sleep 3")
END {
  "date" | getline dt
  close("date")
  print "end:", dt
  }' 
start: Fri Nov  6 15:47:23     2009
end: Fri Nov  6 15:47:27     2009


It works! Thank you very much dude!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print the string between 3rd and 4th backslashs to end of line

im trying to get awk to print the string between 3rd and 4th backslashs to end of line test could be any word this http://example.com/test/ >to this http://example.com/test/ > testalso the other way round insert string at end of line... (13 Replies)
Discussion started by: bob123
13 Replies

2. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

3. Shell Programming and Scripting

awk one liner to print to end of line

Given: 1,2,whatever,a,940,sot how can i print from one particular field to the end of line? awk -F"," '{print $2 - endofline}' the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens: 1---2---whatever---a---940---sot (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

Find and print specific date with awk

hi all I would like to help me find the problem with this script to find and print to the screen a specific date of a log file that I have on my server, the date it is received as the first argument in the script $ 1 Here I show you a few lines that made ​​the idea of ​​my log file: ****... (4 Replies)
Discussion started by: gilmore666
4 Replies

5. Shell Programming and Scripting

strange: sed and awk print at end instead of begin of line

Hi! I have a strange behaviour from sed and awk, but I'm not sure, if I'm doing something wrong: I have a list of words, where I want to add the following string at the end of each line: \;\;\;\;0\;1 I try like this: $ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}' Result:... (5 Replies)
Discussion started by: regisl67
5 Replies

6. Shell Programming and Scripting

Print required values at end of the file by using AWK

I am looking help in awk, quick overview. we will get feed from external system . The input file looks like below. Detail Id Info Id Order Id STATUS Status Date FileDetail 99127942 819718 CMOG223481502 PR 04-17-2011 06:01:34PM... (7 Replies)
Discussion started by: dvrbabu
7 Replies

7. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

8. Shell Programming and Scripting

using awk to print date

i want to use awk to print the first,second and add date as third column in a file awk -F"|" ' { print $1,$2,current date }' tom.unl >> top.txt how can i achieve this,i need the comma's to seperate them and finally print current date and time as the third. i want output like... (5 Replies)
Discussion started by: tomjones
5 Replies

9. UNIX for Dummies Questions & Answers

awk to print current date?

List, I want to print the first line of my text file (say "me you"), preceded by the current date/time. Something like (pseudo code): awk '{print date,$1}' I don't have a lot of awk knowledge (understatement), so forgive me if the answer is obvious... (3 Replies)
Discussion started by: philipz
3 Replies

10. Shell Programming and Scripting

if clause in AWK END block not working.

Hello all... I have a slight problem in my awk script... I have a script which checks a csv file and keeps a count of any invalid records and then if it finds any, exits with a code of 1. problem is it dosnt seem to work properly :rolleyes: Everthing seem to work interms of the stats output,... (1 Reply)
Discussion started by: satnamx
1 Replies
Login or Register to Ask a Question