Date in awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date in awk command
# 1  
Old 02-06-2015
Date in awk command

Hello guys. i have problem to execute the:
Code:
DT=$(date +"%Y%m%d");

command in awk script. i wrote this script:
Code:
#bin/bash
BEGIN{
    FS="[_-]"
    DT=$(date +"%Y%m%d");
    FullBackupValue=0;
    LevelBackupValue=0;
    ControlBackupValue=0;
    ArchBackupValue=0;
}
{
    if($1 == "full" && $3==DT){
    FullBackupValue++;
    }
    else if($1 == "level1" && $3==DT){
    LevelBackupValue++;
    }
    else if($1 == "c" && $3==DT){
    ControlBackupValue++;
    }
    else if($1 == "arch" && $3==DT){
    ArchBackupValue++;
    }
}
END{
    print DT;
    printf("OK|");
    printf("%s=%d ","FullBackup",FullBackupValue);
    printf("%s=%d ","LevelBackup",LevelBackupValue);
    printf("%s=%d ","ControlBackup",ControlBackupValue);
    printf("%s=%d ","ArchBackup",ArchBackupValue);
}

i set DT value manually and it works fine but when i try to set it automatically(like the above code) it doesn't work.
would you please help me?
# 2  
Old 02-06-2015
Hello Ymir,

As you haven't mentioned your requirement so can't tell which output you are expecting, but following is the corrected script which I got from your post, could you please run it and let us know if that helps, if not kindly let us know the complete requirement with expected output details.
Code:
#bin/bash
echo $1 | awk '
BEGIN{
    FS="[_-]"
    DT=$(date +"%Y%m%d");
    FullBackupValue=0;
    LevelBackupValue=0;
    ControlBackupValue=0;
    ArchBackupValue=0;
}
{
    if($1 == "full" && $3==DT){
    FullBackupValue++;
    }
    else if($1 == "level1" && $3==DT){
    LevelBackupValue++;
    }
    else if($1 == "c" && $3==DT){
    ControlBackupValue++;
    }
    else if($1 == "arch" && $3==DT){
    ArchBackupValue++;
    }
}
END{
    print DT;
    printf("OK|");
    printf("%s=%d ","FullBackup",FullBackupValue);
    printf("%s=%d ","LevelBackup",LevelBackupValue);
    printf("%s=%d ","ControlBackup",ControlBackupValue);
    printf("%s=%d ","ArchBackup",ArchBackupValue);
}'

Thanks,
R. Singh
# 3  
Old 02-06-2015
You can't assign an awk - variable from a shell command like that. awk will accept
- a parameter like awk -vDT=$(date +"%Y%m%d) '...'
- getline from a command like awk 'BEGIN{"date +%Y%m%d" | getline DT; print DT}'
# 4  
Old 02-06-2015
Quote:
Originally Posted by RudiC
You can't assign an awk - variable from a shell command like that. awk will accept
- a parameter like awk -vDT=$(date +"%Y%m%d) '...'
- getline from a command like awk 'BEGIN{"date +%Y%m%d" | getline DT; print DT}'
thank you so much dear RudiC.
you really are a lifesaver. did you know that? Smilie
# 5  
Old 02-06-2015
Thanks.
No I didn't. Its just about reading (and applying) man pages etc...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

3. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

4. Shell Programming and Scripting

Help in using date command inside awk

Hi All, bash-3.2$ autorep -J BOX_NAME% -l0 | grep BOX_NAME| awk -f awkScript.awk sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: syntax error: unexpected end of file BOX_NAME SU 06/21/2013 03:44:03 06/21/2013 07:46:37 0 #My awkfile { ... (3 Replies)
Discussion started by: ddspark
3 Replies

5. Shell Programming and Scripting

Help on awk and grep command to get the date

Hi All I need to get the date from a file. my file a.out has the below data: INFO : LM_36435 : (10153|1400211776) Starting execution of workflow in folder last saved by user . The date here is Sun Jun 17 05:00:05 2013 ,which is the system date and keep on changing everyday. So... (8 Replies)
Discussion started by: aliva Dash
8 Replies

6. Shell Programming and Scripting

awk - append date to output of a command

Hi all; I am running a script:/var/tmp/gcsw -ne | grep "State:2" | wc that gives me output like:80 480 6529 but i need this output as:2013-01-18 13:00 -> 80 480 6529 (1 Reply)
Discussion started by: gc_sw
1 Replies

7. Shell Programming and Scripting

awk command with date

hi, I have one file /home/user/file1 which entry is below cat /home/user/file1 /tmp/test1 /tmp/test2 /tmp/test3 now I want to create a new file with name /home/user/file2 with date as suffix after each entry. like cat /home/user/file2 /tmp/test1_`date "+%Y-%m-%d"`... (5 Replies)
Discussion started by: anshu ranjan
5 Replies

8. Shell Programming and Scripting

Using awk with the date command and escape characters

I have a file that is a log file for web traffic. I would like to convert the timestamp in it to unix time or epoch time. I am using the date command in conjunction with awk to try to do this. Just myfile: 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813 28/Aug/1995:00:00:38 1... (3 Replies)
Discussion started by: jontjioe
3 Replies

9. Shell Programming and Scripting

Separate date timestamp use awk or sed command ?

Hi, I have logfile like this : Actually the format is date format : yyyymmddHHMMSS and i want the log become this format yyyy-mm-dd HH:MM:SS for example 2009-07-19 11:46:52 Can somebody help me ? Thanks in advance (3 Replies)
Discussion started by: justbow
3 Replies

10. Shell Programming and Scripting

Using awk and current date command

I am running a command to extract data from a database which has different text and dates (ex. 01/03/07, 05/29/08, 06/05/08). Once the file is created I need to grep for all the text with a current date then redirect the sorted data to another file then email the file out. Here is what I have right... (2 Replies)
Discussion started by: wereyou
2 Replies
Login or Register to Ask a Question