Add current date to the data file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add current date to the data file
# 1  
Old 11-02-2012
Add current date to the data file

hi all Please assist i need to add current date as data to load to a database:
the script is as follow:

Code:
#!/bin/ksh

DIR=/export/home/yani_m/scripts/scrip_out_put/
DIR2=/export/home/yani_m/scripts/scrip_out_put/calc/
Date=$1
File22="disk_"$Date
Server=$2
iofiles=$DIR"/*"$Date"*_Ynr.dat"
gzipedfile=$File22".gz"

Code:
#SERVER2C       : ./test.ksh 20120716 NLEMM02
grep  c[0-9]t[0-9]d[0-9] $iofiles >> $DIR2/$File22
grep $Server $DIR2/$File22 |cut -d : -f 2 |cut -d , -f 1,2,9,10 | nawk -F "," 'BEGIN {sumr=0;sumw=0;busy=0;wait=0} {sumr=sumr+$1}{sumw=sumw+$2}{wait=wait+$3}{busy=busy+$4} {maxr=$1 ; maxw=$2;maxb=$4 ;maxwe=$3} $1>=maxr {maxr = $1} $2>=ma
xw {maxw = $2} $3>=maxwe {maxwe = $3} $4>=maxb {maxb = $4} END {print sumr/NR "," sumw/NR ","  busy/NR "," wait/NR "," maxr "," maxw "," maxb "," maxwe ","}'>>/export/home/yani_m/scripts/FILE_TO_LOAD.dat
gzip $DIR2/$File22


I want the print function to also print the date in the file.
Content of the file:
Code:
0.0230769,0.407692,0.307692,0,0.1,1.4,1,0,ADD DATE HERE,


Last edited by Scrutinizer; 11-02-2012 at 05:14 AM.. Reason: code tags
# 2  
Old 11-02-2012
If you give the date as the first parameter to the script you can parse it to awk:

Code:
#SERVER2C       : ./test.ksh 20120716 NLEMM02
grep  c[0-9]t[0-9]d[0-9] $iofiles >> $DIR2/$File22
grep $Server $DIR2/$File22 |cut -d : -f 2 |cut -d , -f 1,2,9,10 | nawk -F "," -v date=$1 'BEGIN {sumr=0;sumw=0;busy=0;wait=0} {sumr=sumr+$1}{sumw=sumw+$2}{wait=wait+$3}{busy=busy+$4} {maxr=$1 ; maxw=$2;maxb=$4 ;maxwe=$3} $1>=maxr {maxr = $1} $2>=ma
xw {maxw = $2} $3>=maxwe {maxwe = $3} $4>=maxb {maxb = $4} END {print sumr/NR "," sumw/NR ","  busy/NR "," wait/NR "," maxr "," maxw "," maxb "," maxwe "," date}'>>/export/home/yani_m/scripts/FILE_TO_LOAD.dat
gzip $DIR2/$File22

# 3  
Old 11-02-2012
im using nawk..its not workin..

---------- Post updated at 02:20 PM ---------- Previous update was at 10:53 AM ----------

I want this:
0.0230769,0.407692,0.307692,0,0.1,1.4,1,0,ADD DATE HERE,

im getting this:
11/02/12
0.00192308,0.0269231,0.0192308,0,0.1,1.4,1,0,

my script:
Code:
#!/bin/ksh

DIR=/export/home/yani_m/scripts/scrip_out_put/
DIR2=/export/home/yani_m/scripts/scrip_out_put/calc/
Date=$1
File22="disk_"$Date
Server=$2
iofiles=$DIR"/*"$Date"*_Ynr.dat"
gzipedfile=$File22".gz"

#SERVER2C       : ./test.ksh 20120716 NLEMM02
grep  c[0-9]t[0-9]d[0-9] $iofiles >> $DIR2/$File22
grep $Server $DIR2/$File22 |cut -d : -f 2 |cut -d , -f 1,2,9,10 | nawk -F "," 'BEGIN {Date=system("date +%D")}{sumr=0;sumw=0;busy=0;wait=0}{sumr=sumr+$1}{sumw=sumw+$2}{wait=wait+$3}{busy=busy+$4}{maxr=$1 ; maxw=$2;maxb=$4 ;maxwe=$3}$1>=m
axr {maxr = $1} $2>=maxw {maxw = $2}$3>=maxwe {maxwe = $3}$4>=maxb {maxb = $4}
END{print sumr/NR "," sumw/NR ","  busy/NR "," wait/NR "," maxr "," maxw "," maxb "," maxwe ","}'>>/export/home/yani_m/scripts/FILE_TO_LOAD.dat
gzip $DIR2/$File22


please help guys

Last edited by Franklin52; 11-02-2012 at 10:03 AM.. Reason: Code tags
# 4  
Old 11-02-2012
Quote:
Originally Posted by LucyYani
...
I want this:
0.0230769,0.407692,0.307692,0,0.1,1.4,1,0,ADD DATE HERE,

im getting this:
11/02/12
0.00192308,0.0269231,0.0192308,0,0.1,1.4,1,0,

my script:
Code:
#!/bin/ksh
 
DIR=/export/home/yani_m/scripts/scrip_out_put/
DIR2=/export/home/yani_m/scripts/scrip_out_put/calc/
Date=$1
File22="disk_"$Date
Server=$2
iofiles=$DIR"/*"$Date"*_Ynr.dat"
gzipedfile=$File22".gz"
 
#SERVER2C       : ./test.ksh 20120716 NLEMM02
grep  c[0-9]t[0-9]d[0-9] $iofiles >> $DIR2/$File22
grep $Server $DIR2/$File22 |cut -d : -f 2 |cut -d , -f 1,2,9,10 | nawk -F "," 'BEGIN {Date=system("date +%D")}
{sumr=0;sumw=0;busy=0;wait=0}{sumr=sumr+$1}{sumw=sumw+$2}{wait=wait+$3}{busy=busy+$4}{maxr=$1 ; maxw=$2;maxb=$4 ;maxwe=$3}$1>=m
axr {maxr = $1} $2>=maxw {maxw = $2}$3>=maxwe {maxwe = $3}$4>=maxb {maxb = $4}
END{print sumr/NR "," sumw/NR ","  busy/NR "," wait/NR "," maxr "," maxw "," maxb "," maxwe ","}'>>/export/home/yani_m/scripts/FILE_TO_LOAD.dat
gzip $DIR2/$File22

...
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.sh
bash 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
$
$

tyler_durden
# 5  
Old 11-02-2012
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

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)
Discussion started by: pradeepp
3 Replies

2. Shell Programming and Scripting

Subtract a file's modification date with current date

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)
Discussion started by: roshanbi
2 Replies

3. Shell Programming and Scripting

Add current date and time

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)
Discussion started by: radius
10 Replies

4. Shell Programming and Scripting

[Solved] How to tar data along with current system date and time.?

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)
Discussion started by: manalisharmabe
10 Replies

5. UNIX for Dummies Questions & Answers

Unable to add date to current date.

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)
Discussion started by: amit.mathur08
11 Replies

6. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

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)
Discussion started by: chumsky
4 Replies

7. Shell Programming and Scripting

How to extract log data based on current date and month ?

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)
Discussion started by: superHonda123
5 Replies

8. Shell Programming and Scripting

how to add current date to the file with sed

I want to add current date into the begin of the first line of the file. please show me the way please with "date" commond and sed (4 Replies)
Discussion started by: yanglei_fage
4 Replies

9. Shell Programming and Scripting

how to create file.txt and add current date in file content

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)
Discussion started by: chenboly
3 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

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)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question

Featured Tech Videos