2

I have a question related to awk command in shell. We have a input csv file with following values:

Control_Time;Report_Name 2020-08-10;Report A 2020-06-10;Report B 2020-07-01;Report C 

My goal is to check all the rows in this file and filter (copy) only the rows which have a date greater than first day of previous month. So, I created an variable called previous_month which is following:

previous_month=$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d) 

This variable works fine, but I am not able to work with it in awk statement which I want to use for filtering data into the output file.

I tried following statement just for printing the previous_month and it is not working.

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}' 

However, echo previous_month works fine and it gives me the value 2020-07-01. Is there any way how to work with this custom variable? Thank you for all advices!

1
  • If you show us the code you need help with, i.e. the code you want to use for filtering data into the output file, then we can help you debug and improve that code. Commented Aug 17, 2020 at 13:50

3 Answers 3

4

You were close, could you please try following.

awk -v pre_date=$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d) ' BEGIN{ FS=OFS=";" } $1==pre_date ' Input_file 

Improvements in OP's code attempts:

  • We could create an awk variable where we could mention date shell command itself, so taken OP's command of date and placed it inside pre_date awk variable.
  • Since OP wants to match previous month's date with Input_file so first we need to set delimiter as ; as per OP's sample Input_file.
  • Then we need to compare $1 1st field of Input_file with awk variable and check condition if both are same then no action mentioned so by default print of current line will happen.
Sign up to request clarification or add additional context in comments.

Comments

2

Your command works just fine. awk is just working on lines either given by stdin or by a file. Or what you're typing.

Try the following:

echo "hello world" | awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}' 

This will print the date once, since you provided one line via stdin.

Or

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}' filename 

This will print the date for each line in the file.

Or

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{print previous_month}' 

(and now type something and press enter)

This will print the date as well each time you press enter.

Comments

0

thank you both for your answers. It was really helpful! I did not realized, that I need to use the input file. So, it worked fine, but I had to add input file.

Final query is following:

awk -v previous_month="$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)" '{if ($1 > previous_month) print $0}' inputFile > outputFile; 

It could help to someone else.

1 Comment

'{if ($1 > previous_month) print $0}' should just be '$1 > previous_month', nothing else.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.