-1

I use this command directly on our redhat linux server 8.8 and it's working correctly and I get the result I want:

grep '01-FEB-2024' /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }'

I need to automotize this procedure with a bash file and it looks like that it doesn't get the value of current_date variable:

#!/bin/bash
current_date=$(date "+%d-%b-%Y")
grep '$current_date' /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }' >> y.out
grep "$current_date" /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }' >> y.out
grep $current_date /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }' >> y.out

In all of these cases it return an empty value. Thank you in advance.

2
  • 1
    date "+%d-%b-%Y" will return 06-Feb-2024, not 06-FEB-2024, so consider using grep -i for case insensitivity
    – Panki
    Commented Feb 6, 2024 at 14:10
  • It worked perfectly with -i. Thank you very much. If you post it I'll accept your answer.
    – TimLer
    Commented Feb 6, 2024 at 14:13

3 Answers 3

4

date +%d-%b-%Y outputs the current date with the day of the month 0-padded to a length of 2, the user's locale month abbreviation, and the year 0-padded to 4 digits.

Depending on who runs that command, you may get something like:

06-فبر-2024
06-лют-2024
06-fév-2024

In the C/POSIX locale, you get:

$ LC_ALL=C date +%d-%b-%Y
06-Feb-2024

It looks like your log contains the same but in uppercase.

Here, you could do:

#! /bin/sh -
TODAY=$(LC_ALL=C date +%d-%b-%Y) exec awk '
  BEGIN{today = toupper(ENVIRON["TODAY"])}
  index($0, today) && $NF != 0
  ' /u01/app/server1/listener_scan/trace/listener_scan.log

mawk, busybox awk and gawk can get you that information by themselves, so on a Linux-based systems this is also very likely to work:

#! /bin/sh -
LC_ALL=C exec awk '
  BEGIN {today = toupper(strftime("%d-%b-%F"))}
  index($0, today) && $NF != 0
  ' /u01/app/server1/listener_scan/trace/listener_scan.log

(that also affects the way the contents of the log file is decoded into text, but that's likely for the best; that also affects the language of error messages if any)

4
  • Why TODAY UPPER CASE? Looks like they are used by system variables, maybe better to not using those? Commented Feb 6, 2024 at 14:40
  • @GillesQuénot because that's passed as an environment variable to awk (thanks for the edit btw) Commented Feb 6, 2024 at 14:42
  • Then, awk -v today=xxx looks more the way to go. Commented Feb 6, 2024 at 14:43
  • 1
    @GillesQuénot, I try to avoid -v in general as it mangles backslashes (not a problem here). Commented Feb 6, 2024 at 14:43
1

What I would do:

#!/bin/bash

current_date=$(date "+%d-%b-%Y")
grep -i "$current_date" /u01/app/server1/listener_scan/trace/listener_scan.log |
    awk '{ if ( $NF != 0 ) print $0 }' >> y.out

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
https://web.archive.org/web/20230224010517/https://wiki.bash-hackers.org/syntax/words
when-is-double-quoting-necessary

0
0

You need to match on a pattern such as 06-FEB-2024, but as was noted in a comment, the date command returns 06-Feb-2024.

You can upper-case the variable:

current_date=$(LC_ALL=C date "+%d-%b-%Y")
grep "${current_date^^}" /u01/app/server1/listener_scan/trace/listener_scan.log |
    awk '{ if ( $NF != 0 ) print $0 }' >> y.out

Also you can merge the combination:

awk -v date="$(LC_ALL=C date +'%d-%b-%Y')" '$0 ~ toupper(date) && $NF!=0' /u01/app/server1/listener_scan/trace/listener_scan.log >>y.out
2
  • 1
    GNU date has an "upper case if possible" format flag ^ so you could use %^b in that case I think Commented Feb 6, 2024 at 14:55
  • $NF+0 is not the same as $NF != 0. It would fail to return lines that end in foo for instance. as by applying an arithmetic operation, you're converting $NF to a number Commented Feb 6, 2024 at 15:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.