awk command with date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command with date
# 1  
Old 09-20-2011
awk command with date

hi,

I have one file /home/user/file1 which entry is below

HTML Code:
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
HTML Code:
cat /home/user/file2
/tmp/test1_`date "+%Y-%m-%d"`
/tmp/test2_`date "+%Y-%m-%d"`
/tmp/test3_`date "+%Y-%m-%d"`
I used awk command but I think awk is not taking date command and its giving error like
HTML Code:
awk: {print $1"_"date+%Y-m-d""}
awk:                  ^ syntax error
Could you please suggest me how to use awk command?

Thanks
# 2  
Old 09-20-2011
Either I dont understand what you are up to, either the content of file2 is not what it should be...
What do you want to do with awk?
# 3  
Old 09-20-2011
Not too sure either what you plan to do with it, but you can get the desired output with sed.

Code:
$ sed 's/$/_`date "+%Y-%m-%d"`/' file1 > file2
/tmp/test1_`date "+%Y-%m-%d"`
/tmp/test2_`date "+%Y-%m-%d"`
/tmp/test3_`date "+%Y-%m-%d"`

A fix for your awk:
Code:
$ awk '{print $0 "_`date \"+%Y-%m-%d\"`"}' file1
/tmp/test1_`date "+%Y-%m-%d"`
/tmp/test2_`date "+%Y-%m-%d"`
/tmp/test3_`date "+%Y-%m-%d"`


Last edited by Scott; 09-20-2011 at 10:11 AM.. Reason: Added AWK
# 4  
Old 09-20-2011
Code:
awk 'BEGIN{cmd="date +%Y-%m-%d";cmd|getline;date=$0}{print $0"_"date }' file1 >> file2

--ahamed

---------- Post updated at 06:21 AM ---------- Previous update was at 06:15 AM ----------

or

Code:
awk -v date=`date +%Y-%m-%d` '{print $0"_"date }' file1 >> file2

--ahamed
# 5  
Old 09-20-2011
It wasn't clear if the intention was to have the actual date or the output as shown.

For the former, it would sure be more efficient just to run date once?

Code:
D=$(date "+%Y-%m-%d")
sed "s/$/_$D/" file1 > file2

# 6  
Old 09-26-2011
Thanks a lot every one. I got my result.
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

Date in awk command

Hello guys. i have problem to execute the: DT=$(date +"%Y%m%d"); command in awk script. i wrote this script: #bin/bash BEGIN{ FS="" DT=$(date +"%Y%m%d"); FullBackupValue=0; LevelBackupValue=0; ControlBackupValue=0; ArchBackupValue=0; } { if($1 == "full" &&... (4 Replies)
Discussion started by: Ymir
4 Replies

4. 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

5. 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

6. 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

7. 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

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