LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2018, 06:52 AM   #1
nish43
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
Cant input date variable in awk


I have a directory /user/reports under which many files are there, one of them is :

Code:
report.active_user.30092018.77325.csv
I need output as number after date i.e. 77325 from above file name.

I created below command to find a value from file name:

Code:
ls /user/reports | awk -F. '/report.active_user.30092018/ {print $(NF-1)}'
Now, I want current date to be passed in above command as variable and get result:

Code:
ls /user/reports | awk -F. '/report.active_user.$(date +'%d%m%Y')/ {print $(NF-1)}'
But not getting required output.

Tried bash script:

Code:
#!/usr/bin/env bash

_date=`date +%d%m%Y`

 active=$(ls /user/reports | awk -F. '/report.active_user.${_date}/ {print $(NF-1)}')


 echo $active
But still output is blank.

Please help with proper syntax.
 
Old 09-30-2018, 08:51 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
something like this maybe?
Code:
#shows files in directory
userx@slackcurrent:~/testdir$ for i in $(ls) ; do echo $i ; done
report.active_user.30092018.77320.csv
report.active_user.30092018.77321.csv

shows results in getting just the digits to the right of date

userx@slackcurrent:~/testdir$ for i in $(ls) ; do f=${i%.*} ; f=${f##*.} ; echo $f ; done
77320
77321

Last edited by BW-userx; 09-30-2018 at 08:52 AM.
 
1 members found this post helpful.
Old 09-30-2018, 09:19 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Also keep track of the quoting. The single quotes ' ' will provide the contents literally. The double quotes " " will have their contents interpreted. Compare "$a" versus '$a' in the shell.
With double quotes you'll have to escape any dollar signs that you want passed to AWK as literals. Either way enclosing the same quotes inside the same quotes needs escaping too, as in "\"\"" and '\'\''

Maybe pass the date as a variable using -v or --assign

Code:
awk -v d=$(date +"%d%m%Y") 'BEGIN{p="report.active_user."d} $0~p{ print $(NF-1)}' nish43-01.data

Last edited by Turbocapitalist; 09-30-2018 at 09:24 AM. Reason: escaped quotes must have backslash escaped explicitly
 
Old 10-01-2018, 02:17 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Or you could try and learn how awk deals with dates:

https://www.gnu.org/software/gawk/ma...Time-Functions
 
1 members found this post helpful.
Old 10-01-2018, 02:33 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by grail View Post
Or you could try and learn how [g]awk deals with dates:
It's not portable:

Quote:
They are gawk extensions; they are not specified in the POSIX standard.
However, that may not matter in some cases.
 
Old 10-01-2018, 03:26 AM   #6
nish43
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by BW-userx View Post
something like this maybe?
Code:
#shows files in directory
userx@slackcurrent:~/testdir$ for i in $(ls) ; do echo $i ; done
report.active_user.30092018.77320.csv
report.active_user.30092018.77321.csv

shows results in getting just the digits to the right of date

userx@slackcurrent:~/testdir$ for i in $(ls) ; do f=${i%.*} ; f=${f##*.} ; echo $f ; done
77320
77321
This worked .. Thanks everyone for quick response.
 
Old 10-02-2018, 10:42 AM   #7
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Your original attempt should work just fine if you reposition two single-quotes.

Code:
ls /user/reports | awk -F. '/report.active_user.'$(date +%d%m%Y)'/ {print $(NF-1)}'
 
  


Reply

Tags
awk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to get yesterdays date using the date command in a cronjob or cron variable malbe Linux - Newbie 10 05-24-2018 07:39 PM
How to pass DATE variable & search with it using AWK ? ssathishkumar Linux - Newbie 4 12-26-2017 06:54 AM
problem in comparing awk field variable with stored value variable lalith.145 Programming 2 01-15-2015 08:46 PM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration