Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to use bash variable inside filter verb? #209

Closed
aborruso opened this issue Dec 9, 2018 · 12 comments
Closed

How to use bash variable inside filter verb? #209

aborruso opened this issue Dec 9, 2018 · 12 comments

Comments

@aborruso
Copy link
Contributor

aborruso commented Dec 9, 2018

Hi,
I have this kind of script

# a date
treGiorniFa=$(date -d "2 days ago" '+%Y-%m-%d')

# my filter
mlr --csv filter 'strptime($data, "%Y-%m-%d") < strptime('"$treGiorniFa"', "%Y-%m-%d")' ./MAria_report.csv

But it does not work. How can I pass a bash variable value to a filter expression?

Thank you

@johnkerl
Copy link
Owner

johnkerl commented Dec 9, 2018

that looks right. can you bash -xv it and post the output?

@aborruso
Copy link
Contributor Author

aborruso commented Dec 9, 2018

Hi,
I'm not able to save it using bash -xv >./xv.txt :(

This is my error

treGiorniFa=$(date -d "2 days ago" '+%Y-%m-%d')
mlr --csv filter  'strptime($data, "%Y-%m-%d") < strptime('"$treGiorniFa"', "%Y-%m-%d")' ./MAria_report_long.csv
+ mlr --csv filter 'strptime($data, "%Y-%m-%d") < strptime(2018-12-07, "%Y-%m-%d")' ./MAria_report_long.csv
Expression does not evaluate to boolean: got MT_ERROR.

@aborruso
Copy link
Contributor Author

aborruso commented Dec 9, 2018

My input file

data,value,unita,frequenza,inquinante,stazione
2013-01-01,2.227614,(µg/m3),Max orario,SO2,partinico
2013-01-01,0.887651,(µg/m3),Media 24 h,SO2,partinico
2013-01-01,1.349516,(mg/m3),Max media 8h,CO,partinico
2013-01-01,123.068614,(µg/m3),Max orario,NO2,partinico
2013-01-01,34.898269,(µg/m3),Max media 8h,O3,partinico
2013-01-01,22.411990,(µg/m3),Media 24 h,PM10,partinico
2013-01-01,4.194134,(µg/m3),Max orario,Benzene,partinico
2013-01-01,41.493824,(µg/m3),Max orario,Benzene,Marcellino
2013-01-01,0.072567,(µg/m3),Max orario,SO2,enna

@johnkerl
Copy link
Owner

johnkerl commented Dec 9, 2018

Close -- you're only missing a pair of double quotes!

$ cat prima-prova.sh
treGiorniFa=$(date -v-2d '+%Y-%m-%d')
mlr --csv filter  'strptime($data, "%Y-%m-%d") < strptime('"$treGiorniFa"', "%Y-%m-%d")' ./MAria_report_long.csv
$ bash -xv prima-prova.sh
treGiorniFa=$(date -v-2d '+%Y-%m-%d')
date -v-2d '+%Y-%m-%d'
++ date -v-2d +%Y-%m-%d
+ treGiorniFa=2018-12-07
mlr --csv filter  'strptime($data, "%Y-%m-%d") < strptime('"$treGiorniFa"', "%Y-%m-%d")' ./MAria_report_long.csv
+ mlr --csv filter 'strptime($data, "%Y-%m-%d") < strptime(2018-12-07, "%Y-%m-%d")' ./MAria_report_long.csv
Expression does not evaluate to boolean: got MT_ERROR.
$ cat seconda-prova.sh
treGiorniFa=$(date -v-2d '+%Y-%m-%d')
mlr --csv filter  'strptime($data, "%Y-%m-%d") < strptime("'"$treGiorniFa"'", "%Y-%m-%d")' ./MAria_report_long.csv
$ bash -xv seconda-prova.sh
treGiorniFa=$(date -v-2d '+%Y-%m-%d')
date -v-2d '+%Y-%m-%d'
++ date -v-2d +%Y-%m-%d
+ treGiorniFa=2018-12-07
mlr --csv filter  'strptime($data, "%Y-%m-%d") < strptime("'"$treGiorniFa"'", "%Y-%m-%d")' ./MAria_report_long.csv
+ mlr --csv filter 'strptime($data, "%Y-%m-%d") < strptime("2018-12-07", "%Y-%m-%d")' ./MAria_report_long.csv
data,value,unita,frequenza,inquinante,stazione
2013-01-01,2.227614,(µg/m3),Max orario,SO2,partinico
2013-01-01,0.887651,(µg/m3),Media 24 h,SO2,partinico
2013-01-01,1.349516,(mg/m3),Max media 8h,CO,partinico
2013-01-01,123.068614,(µg/m3),Max orario,NO2,partinico
2013-01-01,34.898269,(µg/m3),Max media 8h,O3,partinico
2013-01-01,22.411990,(µg/m3),Media 24 h,PM10,partinico
2013-01-01,4.194134,(µg/m3),Max orario,Benzene,partinico
2013-01-01,41.493824,(µg/m3),Max orario,Benzene,Marcellino
2013-01-01,0.072567,(µg/m3),Max orario,SO2,enna

@johnkerl johnkerl closed this as completed Dec 9, 2018
@johnkerl
Copy link
Owner

johnkerl commented Dec 9, 2018

(The slightly different date syntax is just because I'm using MacOSX.)

@aborruso
Copy link
Contributor Author

Oh, what a stupid error, thank you very much

@aborruso
Copy link
Contributor Author

aborruso commented Jul 8, 2019

@johnkerl another related question, if you have the time for it.

If I run

echo 'input=2017-06-22T16:29:09.718382' | \
mlr --opprint --barred  put -S '$yearmonth="'"$(date -d2017-06-22T16:29:09.718382 +%m)"'"'

I have

+----------------------------+-----------+
| input                      | yearmonth |
+----------------------------+-----------+
| 2017-06-22T16:29:09.718382 | 06        |
+----------------------------+-----------+

How to use input field inside the put verb?

If I run

echo 'input=2017-06-22T16:29:09.718382' | \
mlr --opprint --barred  put -S '$yearmonth="'"$(date -d'$input' +%m)"'"'

The input value is not extracted

+----------------------------+-----------+
| input                      | yearmonth |
+----------------------------+-----------+
| 2017-06-22T16:29:09.718382 | -         |
+----------------------------+-----------+

Thank you

@johnkerl johnkerl reopened this Jul 10, 2019
@johnkerl
Copy link
Owner

#246 would really help out here :)

@aborruso
Copy link
Contributor Author

@johnkerl I have read #246 but I'm not sure I understood it.

Reading it, it seems impossible to do it inside DSL. Am I wrong?

@johnkerl
Copy link
Owner

#246 would make it easier for you to shell out to bash commands without having to play around with single-quotes. The feature doesn't exist yet.

@aborruso
Copy link
Contributor Author

@johnkerl wow, it would be great!!

@johnkerl
Copy link
Owner

johnkerl commented Sep 7, 2019

This is a duplicate of #246

@johnkerl johnkerl closed this as completed Sep 7, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants