LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-26-2017, 06:54 AM   #1
ssathishkumar
LQ Newbie
 
Registered: Jan 2012
Posts: 9

Rep: Reputation: Disabled
How to pass DATE variable & search with it using AWK ?


My file is as below
====================

cat test
ABCDE1020000009,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,
ABCDE1020000012,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171225z
ABCDE1020000011,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226Z
ABCDE1020000008,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171224Z
ABCDE1020000002,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,
ABCDE1020000001,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171223Z
ABCDE1020000004,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171216Z
ABCDE1020000007,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,
ABCDE1020000010,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,
ABCDE0000189991,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226Z

Expected output is:
====================

ABCDE1020000012,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171225z
ABCDE1020000011,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226Z
ABCDE1020000008,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171224Z
ABCDE1020000001,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171223Z
ABCDE1020000004,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171216Z
ABCDE0000189991,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226Z


I want to make the above output using exact date.


awk -F, -v DATE7="$(date +"%Y%m%d" -d "7 days ago")" -v DATE6="$(date +"%Y%m%d" -d "6 days ago")" -v DATE5="$(date +"%Y%m%d" -d "5 days ago")" -v DATE4="$(date +"%Y%m%d" -d "4 days ago")" -v DATE3="$(date +"%Y%m%d" -d "3 days ago")" -v DATE2="$(date +"%Y%m%d" -d "2 days ago")" -v DATE1="$(date +"%Y%m%d" -d "1 days ago")" -v DATE="$(date +"%Y%m%d")" '$5 ~ /"^$DATE7"/"^$DATE6"/"^$DATE5"/"^$DATE4"/"^$DATE3"/"^$DATE2"/"^$DATE1"/"^$DATE"/ {print $0}' test
awk: $5 ~ /"^$DATE7"/"^$DATE6"/"^$DATE5"/"^$DATE4"/"^$DATE3"/"^$DATE2"/"^$DATE1"/"^$DATE"/ {print $0}
awk: ^ syntax error


But its not working, how can i fix it ?
 
Old 12-26-2017, 07:15 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,521
Blog Entries: 3

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
It will help if you wrap your code in [code] [/code] tags so the indentation is preserved. But the double quotes are not needed and the syntax for variables is just the variable name, no dollar sign needed in front. This should be closer:

Code:
awk -F, \
    -v DATE7="$(date +"%Y%m%d" -d "7 days ago")" \
    -v DATE6="$(date +"%Y%m%d" -d "6 days ago")" \
    -v DATE5="$(date +"%Y%m%d" -d "5 days ago")" \
    -v DATE4="$(date +"%Y%m%d" -d "4 days ago")" \
    -v DATE3="$(date +"%Y%m%d" -d "3 days ago")" \
    -v DATE2="$(date +"%Y%m%d" -d "2 days ago")" \
    -v DATE1="$(date +"%Y%m%d" -d "1 days ago")" \
    -v DATE="$(date +"%Y%m%d")" \
    '$5 ~ DATE7 || $5 ~ DATE6 || $5 ~ DATE5 || $5 ~ DATE4 || $5 ~ DATE3 || $5 ~ DATE2 || $5 ~ DATE1 || $5 ~ DATE {print $0}' test
I haven't tested that though.
 
1 members found this post helpful.
Old 12-26-2017, 07:33 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Using grep
Code:
for i in {7..0}; do
  d="$d\|"$(date +"%Y%m%d" -d "$i days ago")
done
grep ",\(${d:2}\).$" test

Last edited by keefaz; 12-26-2017 at 07:44 AM.
 
1 members found this post helpful.
Old 12-26-2017, 07:52 AM   #4
ssathishkumar
LQ Newbie
 
Registered: Jan 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by Turbocapitalist View Post
It will help if you wrap your code in [code] [/code] tags so the indentation is preserved. But the double quotes are not needed and the syntax for variables is just the variable name, no dollar sign needed in front. This should be closer:

Code:
awk -F, \
    -v DATE7="$(date +"%Y%m%d" -d "7 days ago")" \
    -v DATE6="$(date +"%Y%m%d" -d "6 days ago")" \
    -v DATE5="$(date +"%Y%m%d" -d "5 days ago")" \
    -v DATE4="$(date +"%Y%m%d" -d "4 days ago")" \
    -v DATE3="$(date +"%Y%m%d" -d "3 days ago")" \
    -v DATE2="$(date +"%Y%m%d" -d "2 days ago")" \
    -v DATE1="$(date +"%Y%m%d" -d "1 days ago")" \
    -v DATE="$(date +"%Y%m%d")" \
    '$5 ~ DATE7 || $5 ~ DATE6 || $5 ~ DATE5 || $5 ~ DATE4 || $5 ~ DATE3 || $5 ~ DATE2 || $5 ~ DATE1 || $5 ~ DATE {print $0}' test
I haven't tested that though.
Thanks a lot. Working perfectly.

awk -F, \
> -v DATE7="$(date +"%Y%m%d" -d "7 days ago")" \
> -v DATE6="$(date +"%Y%m%d" -d "6 days ago")" \
> -v DATE5="$(date +"%Y%m%d" -d "5 days ago")" \
> -v DATE4="$(date +"%Y%m%d" -d "4 days ago")" \
> -v DATE3="$(date +"%Y%m%d" -d "3 days ago")" \
> -v DATE2="$(date +"%Y%m%d" -d "2 days ago")" \
> -v DATE1="$(date +"%Y%m%d" -d "1 days ago")" \
> -v DATE="$(date +"%Y%m%d")" \ '$5 ~ DATE7 || $5 ~ DATE6 || $5 ~ DATE5 || $5 ~ DATE4 || $5 ~ DATE3 || $5 ~ DATE2 || $5 ~ DATE1 || $5 ~ DATE {print $0}' test

ABCDE1020000012,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,2017122583620Z
ABCDE1020000011,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226183620Z
ABCDE1020000008,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171224183620Z
ABCDE1020000001,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171223183620Z
ABCDE0000189991,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226183620Z
 
Old 12-26-2017, 07:54 AM   #5
ssathishkumar
LQ Newbie
 
Registered: Jan 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by keefaz View Post
Using grep
Code:
for i in {7..0}; do
  d="$d\|"$(date +"%Y%m%d" -d "$i days ago")
done
grep ",\(${d:2}\).$" test
Gr8, this also a good one.

for i in {7..0}; do d="$d\|"$(date +"%Y%m%d" -d "$i days ago"); done | grep ${d:2} test
ABCDE1020000012,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,2017122583620Z
ABCDE1020000011,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226183620Z
ABCDE1020000008,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171224183620Z
ABCDE1020000001,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171223183620Z
ABCDE0000189991,FALSE,SGSNYA101.mme.epc.mncXXX.mccYYY.3gppnetwork.org,mme.epc.mncXXX.mccYYY.3gppnetw ork.org,20171226183620Z

but i want to make it with awk and Turbocapitalist gave the right command. Thanks for your suggestions
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Cannot pass bash variable to awk (in order to cut out text) readmore Programming 3 01-01-2014 05:45 AM
[SOLVED] Awk: How to pass/store a command output in variable? shivaa Linux - Newbie 8 01-05-2013 01:30 PM
[SOLVED] Pass search results to awk, and use awk output to search other files bspears1 Linux - Newbie 8 07-21-2012 10:17 AM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 11:12 AM
how to pass an external variable to awk??? akitty Programming 7 11-13-2008 09:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration