1

I am listing the files/directories which are greather than N days using the below commands

DATE=`date +%Y-%m-%d`
dt=`date --date "$dt" +%Y%m%d`
loop_dt=`date -I --date "$dt -1 day"`   
*** output of loop_dt = 2018-02-25***

hdfs dfs -ls r /path/ | awk '$6 < "$loop_dt"'

I know the above hdfs command is wrong, But I want to pass the loop_dt varible in awk command, to know the list the files which are older than n days

Note: if I hardcode the date in awk command I am getting the results

Allan
  • 12,117
  • 3
  • 27
  • 51
user8587005
  • 65
  • 1
  • 2
  • 8
  • 1
    Check this post: https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-from-a-command-in-bash – codeforester Feb 26 '18 at 06:12
  • As mentioned by coddeforester be cautious about the **use of simple quotes and double quotes** in shells! Also in general, when you passe a variable to `awk` use this syntax: `awk -v awkVarName="$shellVariable" 'BEGIN {print awkVarName}'` – Allan Feb 26 '18 at 06:26

1 Answers1

0

To recap what I have said in the comments you need to fix your awk command in the following way:

$ cat file
2015-08-01
2015-08-13

$ awk -v var="2015-08-12" '{if( $1 < var"") print}' file                                                                                       
2015-08-01

Replace 2015-08-12 by your shell variable "$loop_dt" and it should work.

Explanations:

  • Use this syntax awk -v awkVarName="$shellVariable" 'BEGIN {print awkVarName}' to pass a variable to awk
  • In if( $1 < var"") the "" force the string comparison.
Allan
  • 12,117
  • 3
  • 27
  • 51
  • hi in my scenario I don't want to print anything. I want to list the files based on date condition I am giving in the condition equal to $6( which is a date field ) hdfs dfs -ls -R /path/ | awk '$6 < "$loop_dt"' – user8587005 Feb 26 '18 at 07:38