The built-in "system" function of awk executes the command passed to it as a parameter. Hence the date is printed by your script.
You put the "system" function in the BEGIN section, hence the date was printed before the awk script started its processing.
The return value of the "system" function is the exit status of the command it executes. Since the "date" command was successful, "system" returned 0 and that was assigned to "Date" variable. Hence you see "0" in your output.
Here's another example:
Code:
$$ # my dummy file that will be used by my simple awk script$ cat -n f73 1 AAA 10 2 BBB 20 3 CCC 30$$$ # my shell script$ cat -n f73_test.sh 1 #!/usr/bin/bash 2 3 # 4 # The "system" function runs the host (Unix/Linux) command 5 # and prints the output, if any. 6 # The awk script sums up the 2nd column in file "f73" and 7 # prints it at the end. 8 # 9 awk ' BEGIN { dt = system("type bash") } 10 { sum += $2 } 11 END { print sum","dt } 12 ' f73 13$$ ./f73_test.shbash is /usr/bin/bash <== this came from "BEGIN"60,0 <== and the "0" came from "END"$$
As pointed out by Subbeh, you may want to pass the date variable to your awk script. Thereby you'll be able to print it.
Like so -
Code:
$$ # another shell script working on the same data file "f73"$$ cat -n f73_test1.sh 1 #!/usr/bin/bash 2 3 # capture the 1st parameter in a variable called "mydate" 4 mydate=$1 5 6 # 7 # pass the variable "mydate" to the awk script that sums 8 # the 2nd column in the file "f73" 9 # 10 awk -v dt=$mydate ' { sum += $2 } 11 END { print sum","dt } 12 ' f73 13$$ # invoke the shell script now, passing a parameter$ ./f73_test1.sh `date +%D`60,11/02/12$$
You've been given good examples of how to set the date in your shell script and pass it into your awk script. And for what you seem to be doing here, that is the right thing to do.
If you do actually need to read data inside an awk script from a command created by the script itself, maybe the following example will help you understand how to do it:
Code:
#!/bin/ksh
d_t="$(date "+%D %T")"
sleep 1
d=$(date +%D)
t=$(date +%T) # The timestamp here will be one or two seconds later
sleep 1
nawk -v d_t="$d_t" -v tm="$d" -v dt="$t" 'BEGIN {
# Print the values the shell passed in
print d_t, dt, tm
}
{ # Read commands from the following here-document, run the commands,
# and read the output of those commands a line at a time
printf("Input line %d: command to be run: %s\n", NR, $0)
for(i = 1; $0 | getline out == 1; i++)
printf("Output line %d: %s\n", i, out)
close($0)
}' <<EOF
date +%D;sleep 3
date +%T;sleep 3
date '+%D %T';sleep 3
date '+before%n%c%nafter';sleep 3
cat $HOME/.profile
EOF
We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below:
abc.txt
------------------
Code:
line1
line2
line3
$$EDWS_DATE_INSERT=08-27-2019
line4
$$EDWS_PREV_DATE_INSERT=08-26-2019
I am trying to write a... (3 Replies)
SunOS -s 5.10 Generic_147440-04 sun4u sparc SUNW,SPARC-Enterprise
Hi,
In a folder, there are files. I have a script which reads the current date and subtract the modification date of each file.
How do I achieve this?
Regards,
Joe (2 Replies)
i have file 1.txt
asdas|csada|13|03|10|04|23|A1|canberra
sdasd|sfdsf|13|04|26|23|28|A1|sydney
i want to add today's date and time in the end of each row
expected output
asdas|csada|13|03|10|04|23|A1|canberra|130430|1358
sdasd|sfdsf|13|04|26|23|28|A1|sydney|130430|1358
todays date... (10 Replies)
Hi all,
Following is my small script:-
#!/bin/ksh
for i in `cat /users/jack/mainfile-dr.txt`
do
sudo cp -r $i /users/jack/DR01/.
done
cd /users/jack/DR01/
sudo tar cvf system1-DR.tar *
scp system1-DR.tar backupserver:/DRFiles/system1
sudo rm -rf system1-DR.tar
In this script I... (10 Replies)
Hi,
I am trying to display future date from the current date but unable to do so in UNIX (not in PERL). For eg: if today is March 5 then I want a variable wherein I can store Mar 7 date, but unable to get the future date from the current date.
I have tried many possible ways as mentioned below... (11 Replies)
Hello gurus,
I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4:
NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Hi Gurus,
I'm using HP-UX B.11.23 operating system.
I've been trying to extract this log info based on the current date and month, but was having some issues as the date column which on the 4th column has a comma and the 5th column has a dot tied to it.
Here is the output from my shut... (5 Replies)
Hey guy,
how to make bash script to create foo.txt file and add current date into file content and that file always append.
example: today the script run and add today date into content foo.txt
and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date.
$file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z
really appreciate any help.
thanks
mkneni (4 Replies)