awk variable into shell command "date -d": possible...?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk variable into shell command "date -d": possible...?
# 1  
Old 06-12-2018
awk variable into shell command "date -d": possible...?

Hello, there!
I am trying to pass an awk variable into a shell command ['date'] in order to collect the result into an awk variable; in Bash it does work, as in:

v='2'; date -d "now + $v weeks"

But in awk it does not, as in:

v="2"
"date -d 'now + v weeks'" | getline newdate
close ("date -d 'now + v weeks'")
print newdate


Does anyone have any ideas as to why that is so and/or whether it can be done as I need it?
Thank you all, in advance.

Last edited by fbird3; 06-12-2018 at 05:01 PM..
# 2  
Old 06-12-2018
Code:
v=2
awk -v newdate="$(date -d 'now + $v weeks')" '{print newdate}' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 06-12-2018
Or, to show how it can be done within awk itself:
Code:
awk 'BEGIN{v=2; cmd="date -d \"now + " v " weeks\""; cmd | getline newdate; close(cmd); print newdate}'

The way of quoting is different than in shell..

Or if you put it in a file file.awk:
Code:
BEGIN {
  v=2
  cmd="date -d 'now + " v " weeks'"
  cmd | getline newdate
  close(cmd)
  print newdate
}

Code:
awk -f file.awk


Last edited by Scrutinizer; 06-12-2018 at 05:10 PM..
# 4  
Old 06-12-2018
That's exactly what I was looking for.
Thanks a bunch!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Variable interpolation in "print" of awk command

Hi, I have a snippet like below. Based on variable i, i wish to print 1,2,3,4,5th columns to Sample files. For each loop, one column from contetn and results will be pused to sample files. But i have a problem here i=1 while ; do `awk -F"\t" '{print $($i)}' $content > Sample_${i}_original`;... (4 Replies)
Discussion started by: forums123456
4 Replies

4. Shell Programming and Scripting

Using external "date" command from awk

For some dumb reason, I'm having the strangest hard time getting awk to convert epoch time to UTC human time. I'm using CentOS 5.5 and gawk 3.1.5. There are 2 approaches that I know. First, the strftime call: Tandy400 $ echo 1308607169 | awk '{print strftime("%c",$1)}' Mon 20 Jun 2011... (3 Replies)
Discussion started by: treesloth
3 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question