Inserting date in unix shell with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting date in unix shell with awk
# 1  
Old 06-25-2012
Inserting date in unix shell with awk

Hi Friends,
I have to insert date in a shell script in awk command.,

Shell script which i have written split on file into multiple and rename them based on some values. I want to add timestamp in the file name which is formed in after splitting the files.
Dont know where am doing the mistake as timestamp is not coming in the file name.
Without inserting the timestamp in file names , script is working.
Below is the script command
Code:
awk '/FILEHEADER/{a=$1;sub(".*FILEHEADER","",a);b=$2}/FILEHEADER/,/EOF/{print > a"_Src_"b"`date +"%Y%m%d%H%M%S"`.txt"}' $Inputfile


Last edited by Franklin52; 06-26-2012 at 03:51 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-25-2012
Try passing date string into awk as a variable (DATE):

Code:
awk -vDATE=`date +"%Y%m%d%H%M%S"` '/FILEHEADER/{a=$1;sub(".*FILEHEADER","",a);b=$2}/FILEHEADER/,/EOF/{print > a"_Src_"b DATE ".txt"}' $Inputfile

# 3  
Old 06-26-2012
HI Chubler ,
Thanks for the reply
I modified the script as
Code:
date_n=`date +"%Y%m%d%H%M%S"`
echo $date_n
awk '/FILEHEADER/{a=$1;sub(".*FILEHEADER","",a);b=$2}/FILEHEADER/,/EOF/{print > a"_Src_"b" date_n".txt"}' $inputfilename,

it gave me the eror as
Code:
The error context is
               /FILEHEADER/{a=$1;sub(".*FILEHEADER","",a);b=$2}/FILEHEADER/,/EOF/{print > a"_Src_"b" >>>  date_n". <<< txt"}
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

Can you please help in resolving it

Last edited by Franklin52; 06-26-2012 at 03:51 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 06-26-2012
If you want to use a shell variable to store the date then do it like this:

Code:
date_n=`date +"%Y%m%d%H%M%S"`
 
awk -vDATE="$date_n" '/FILEHEADER/{a=$1;sub(".*FILEHEADER","",a);b=$2}/FILEHEADER/,/EOF/{print > a"_Src_"b DATE ".txt"}' $Inputfile

# 5  
Old 06-26-2012
Hi Chulber ,

I have modified the script based on the one you provide it is not giving the result , it is waiting for some input to be passed Smilie

---------- Post updated at 11:08 PM ---------- Previous update was at 11:04 PM ----------

n awk also didnt worked , same result as for awk
# 6  
Old 06-26-2012
This is most likley because the variable $Inputfile dosn't contain a file path and is probably empty.

Code:
Inputfile="/path/to/your/input/file"
date_n=`date +"%Y%m%d%H%M%S"`
awk -vDATE="$date_n" '/FILEHEADER/{a=$1;sub(".*FILEHEADER","",a);b=$2}/FILEHEADER/,/EOF/{print > a"_Src_"b DATE ".txt"}' $Inputfile

# 7  
Old 06-26-2012
yes, it worked.

Thanks a lot buddy you rocks

---------- Post updated at 11:16 PM ---------- Previous update was at 11:10 PM ----------

how to make this date till milli or even till micro seconds
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

Inserting a row using shell

hi all, In first run,I am inserting a row into an oracle table through shell script, in the same shell script when I query this table the count comes as 0. When I run the same script again then the count comes correctly, Can anyone help how can I get the count of the row correctly in the... (3 Replies)
Discussion started by: Pratiksha Mehra
3 Replies

3. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

4. Shell Programming and Scripting

How to get the consecutive last 10 week day date using UNIX ksh shell scripting?

Hi, i am writing a ksh shell script to check the last month end date whether it is falling in last 10 week day date, I am not sure How to use "Mr. Perderabo's date calculator", Could you Please let me know how to use to get my requirement, I tried my own script but duplicate week day and... (5 Replies)
Discussion started by: karthikram
5 Replies

5. Shell Programming and Scripting

Inserting spaces after a value in UNIX

Hi All, I need to prepare a shell script in UNIX to insert 400 spaces after a variable in a file "file.txt". Eg. if n=T12345 echo $n >> file.txt cat file.txt should give: T12345<400 spaces> Please... (5 Replies)
Discussion started by: learning_skills
5 Replies

6. Shell Programming and Scripting

perl DBI inserting date\time

hi there, my mysql database has a date/time field using the standard mysql date|time format of 2009-08-31 00:16:44 when inserting into this field using perl DBI, is there is an easy way to insert the current date/time in without having to preformat the date/time string in perl before... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

7. UNIX for Advanced & Expert Users

AIX/awk date to unix timestamp

Hello, I am inside a awk script on AIX, I am feeding to awk ls -luNR i need to convert ls -u time format "month day h:m/yr" to Unix epoch time, POSIX time, or aka unix timestamp I do not have strftime funk in my awk, and i have to do this fast meaning that I cannot do a system call in the... (1 Reply)
Discussion started by: nullwhat
1 Replies

8. Shell Programming and Scripting

copy/rename file as date() unix/shell

File.jpg I want to copy and rename this as 2008-12-02.jpg I tried this copy File.jpg date '%y-%m-%d-%H:%M:%S'.jpg This doesnt work.... what do i do? (1 Reply)
Discussion started by: hdogg
1 Replies

9. Shell Programming and Scripting

help in inserting values in date format

how to assign values in this date format a=`date +"%H%M%S"` how to give value of H=22,here in this format so that i can grep 22nd hour.Below is the script -------------------------------------------------------------------------- a=`date +"%H%M%S"` for i in *.log do cat $i | grep $a... (3 Replies)
Discussion started by: ali560045
3 Replies

10. UNIX for Dummies Questions & Answers

Inserting Date&Time Stamp In Existing Log File

I am trying to insert a line with a date stamp in a file that is used to monitor activity in one of our directories. By doing this, I want to grep that file each day and go to the last entry for each time a error occurred and pull all errors generated if any exist. If error exists I want that error... (3 Replies)
Discussion started by: shephardfamily
3 Replies
Login or Register to Ask a Question