Hour display format

Hello Sir/Madam,

I'm using Putty utility to run a shell script. I found HOUR display in two positions in UNIX. Is it possible to display in four positions?

File name example: my file name is: HourlyData_20160708_1400.txt

I'm in the process of comparing current date and time with the file I receive..
my objective is to load the data from the file when date & time has a match

Please suggest how I can achieve that via UNIX shell scrip?

Regards,
Vani Sonti

---------- Post updated at 02:42 PM ---------- Previous update was at 01:08 PM ----------

as requested, here is my shell script ( ps: it's not tested yet )

#!/bin/sh
# Vani Sonti
# shell script date -  07/11/2016 
# commented field code has not been added to this script yet.. 
#fDt=  #- use awk function to trim date from the file name
#fTm= #-- use awk funciton to trim hour from the file name
# hour must be HHHH mode in order to match
dt=`date +"%Y%m%d"`
echo "$dt"
tm=`date +"%H"`
echo "$tm"
filenm = `HourlyData_20160708_1500.txt`
echo "$filenm"
while true
do
 if 
[[ $dt = $fDt && $tm = $fTm ]] then 
# call sqlloader process to parse the file
else
echo no matching file found 
fi

Like this?

FILENAME_WANTED="HourlyDate_$(date +'%Y%m%d_%H%M').txt"
FILENAME_CURRENT='HourlyData_20160708_1500.txt'
if [ "$FILENAME_WANTED" == "$FILENAME_CURRENT" ]; then
   sqlloader $FILENAME_CURRENT
fi

And please notice, that single quotes '...' or double quotes "..." are valid for string encapsulation, but backticks `...` are not. Backticks - which you used in your example - are used for command substitution.

The following is string encapsulation(string is assigned as written):

filenm='HourlyData_20160708_1500.txt'

The following is command substitution(executes the command date and puts the output in its place):

tm=`date +"%H"`
tm=$(date +"%H")

The notation of the two lines result in the same effect. The first is the historical one. The seconde is the newer bash-form. I prefer the latter and never use backticks because of the likeleyhood of confusion to mix the similar looking quotes.

And please notice also that there are no space allowed in variable assignment before and after the equal.

Example:

This is bad(Produces Error "filename": command not found):

filename = 'HourlyData_20160708_1500.txt'

This is also bad(Assigns empty value to variable filename and produces Error 'HourlyData_20160708_1500.txt' command not found):

filename= 'HourlyData_20160708_1500.txt'

This is good:

filename='HourlyData_20160708_1500.txt'

Appreciate your response. Currently i'm invoking shell script using Putty utility so ECHO is helping me to find errors.. I'm new UNIX and not completed tested the script. I'll correct the code based on your examples..

For this project, I'll receive 24 files a day.. (basically one file on top of the hour ).. for that sake I'm trying to compare date and time separably to determine if a match found for those two variables.. if so, load that file.. if not, ignore the load
will I be able to address that with what you described?

Hi stomp,
Given that V1l1h1 wants the hour followed by 00 instead of followed by the current minutes value, we'd need to replace the %M in the date format argument with 00 . And, it is also possible to put the entire string formatting in the date format. Some people find it easier to read this way although the results are the same:

FILENAME_WANTED=$(date +'HourlyDate_%Y%m%d_%H00.txt')

Don - thank you for your suggestion .. I'm modifying shell script to see if it works.. I'll post the results shortly..

In the meantime, how to determine current file.. form the attached list?

one example:

while read LINE;do
  echo $LINE
done < <(ls Hourly*txt)

Are you sure your's is the right approach? Looking at your sample picture (btw, DON'T post pictures but texts so people can work on it!), you would nearly miss HourlyData_20160711_1700.txt as you only have 8 min to detect it, and HourlyData_20160710_1900.txt would slip through entirely. Wouldn't renaming files, optionally into a different directory, be a viable alternative?

if it helps .. file is received hourly.. in the meantime, I'm countering "syntax error at line 14: `fi' unexpected" -- I must be doing something wrong..

#!/bin/sh
FILENAME_WANTED=`date +"HourlyDate_%Y%m%d_%H00.txt"`
echo "$FILENAME_WANTED"
FILENAME_CURRENT='HourlyData_20160708_1500.txt'
echo "$FILENAME_CURRENT"
if [ "$FILENAME_WANTED" == "$FILENAME_CURRENT" ]; then
    echo "file found"
else
    echo "file not found"
fi

A bit strange: "syntax error at line 14" in a 10 line script...? When run with sh , it fails on the == operator. Try with single = only.
And, ...Date... won't match ...Data... .

still erring..

#!/bin/sh
FILENAME_WANTED=`date +"HourlyDate_%Y%m%d_%H00.txt"`
echo "$FILENAME_WANTED"
FILENAME_CURRENT='HourlyData_20160708_1500.txt'
echo "$FILENAME_CURRENT"
if [ "$FILENAME_WANTED" = "$FILENAME_CURRENT" ]; then
    echo "file found"
else
    echo "file not found"
fi

__________________________________________________________

output still has error

perseus.gasleak(/tmp/v_tst)% ls -l
total 40
-rw-r--r--   1 gasleak      2888 Jul 11 15:10 HourlyData_20160708_1500.txt
-rwxrwxrwx   1 gasleak        74 Jul 11 12:29 dt_tm_chk - v1.sh
-rw-r--r--   1 gasleak       229 Jul 11 15:13 dt_tm_chk- 07112016.sh
-rw-r--r--   1 gasleak       397 Jul 12 08:55 dt_tm_chk.sh
-rw-r--r--   1 gasleak       278 Jul 12 09:46 test.sh
perseus.gasleak(/tmp/v_tst)%
perseus.gasleak(/tmp/v_tst)%
perseus.gasleak(/tmp/v_tst)% sh test.sh
HourlyDate_20160712_0900.txt
HourlyData_20160708_1500.txt
test.sh: syntax error at line 10: `fi' unexpected
perseus.gasleak(/tmp/v_tst)%

Worked for me as is. What's your OS/shell/editor version?

i'm using Putty utility to run the shell script .. that is where I'm encountering error..

---------- Post updated at 11:16 AM ---------- Previous update was at 10:34 AM ----------

I used:

ls -t | head -n1

retrieve current file..

out for this is correct:

HourlyData_20160712_1000.txt

will I be able to use command:

 ls -t | head -n1

in a shell script?

The error message you're getting makes no sense for the code you have shown us. Please show us the output (in CODE tags) from the command:

od -bc test.sh

Good morning All -

Rodic,
I'm using notepad to write the shell script and invoke it using Putty utility
that is why I was encountering error "syntax error".. some of the echo lines I had embedded "`" symbol .. therefore, those lines seems to be displaying it ok..

So, I tried via "vi editor" from putty prompt and output displayed correctly for file comparison ( "file not found " ) .. but, editing via vi editor was not easy for me..

Qes. here is: can I use "notepad" to write the script and run it using putty interactively.. should I enclose all "echo" with this "`" symbol?

Hello Don,
This is the file list for today :

perseus.gasleak(/opt/gsoscada/GSO_SCADA)% ls -l Hourl*20160713*.txt
-rw-r--r--   1 gsoscada     1868 Jul 13 00:18 HourlyData_20160713_0000.txt
-rw-r--r--   1 gsoscada     2471 Jul 13 01:18 HourlyData_20160713_0100.txt
-rw-r--r--   1 gsoscada     1732 Jul 13 02:18 HourlyData_20160713_0200.txt
-rw-r--r--   1 gsoscada     2484 Jul 13 03:18 HourlyData_20160713_0300.txt
-rw-r--r--   1 gsoscada     1788 Jul 13 04:18 HourlyData_20160713_0400.txt
-rw-r--r--   1 gsoscada     2484 Jul 13 05:18 HourlyData_20160713_0500.txt
-rw-r--r--   1 gsoscada     1788 Jul 13 06:18 HourlyData_20160713_0600.txt
-rw-r--r--   1 gsoscada     2648 Jul 13 07:18 HourlyData_20160713_0700.txt 

using putty retrieved latest file for today -

perseus.gasleak(/opt/gsoscada/GSO_SCADA)% ls -t | head -n1
HourlyData_20160713_0700.txt

So, my question was that can I use this syntax in the shell script to pull
latest file and stored in a variable for compassion?

by the way, this is what I see for -

perseus.gasleak(/tmp/v_tst)% od -bc test.sh
0000000 043 041 057 142 151 156 057 163 150 015 012 106 111 114 105 116
           #   !   /   b   i   n   /   s   h  \r  \n   F   I   L   E   N
0000020 101 115 105 137 127 101 116 124 105 104 075 140 144 141 164 145
           A   M   E   _   W   A   N   T   E   D   =   `   d   a   t   e
0000040 040 053 042 110 157 165 162 154 171 104 141 164 145 137 045 131
               +   "   H   o   u   r   l   y   D   a   t   e   _   %   Y
0000060 045 155 045 144 137 045 110 060 060 056 164 170 164 042 140 015
           %   m   %   d   _   %   H   0   0   .   t   x   t   "   `  \r
0000100 012 145 143 150 157 040 042 044 106 111 114 105 116 101 115 105
          \n   e   c   h   o       "   $   F   I   L   E   N   A   M   E
0000120 137 127 101 116 124 105 104 042 015 012 106 111 114 105 116 101
           _   W   A   N   T   E   D   "  \r  \n   F   I   L   E   N   A
0000140 115 105 137 103 125 122 122 105 116 124 075 047 110 157 165 162
           M   E   _   C   U   R   R   E   N   T   =   '   H   o   u   r
0000160 154 171 104 141 164 141 137 062 060 061 066 060 067 060 070 137
           l   y   D   a   t   a   _   2   0   1   6   0   7   0   8   _
0000200 061 065 060 060 056 164 170 164 047 015 012 145 143 150 157 040
           1   5   0   0   .   t   x   t   '  \r  \n   e   c   h   o
0000220 042 044 106 111 114 105 116 101 115 105 137 103 125 122 122 105
           "   $   F   I   L   E   N   A   M   E   _   C   U   R   R   E
0000240 116 124 042 015 012 151 146 040 133 040 042 044 106 111 114 105
           N   T   "  \r  \n   i   f       [       "   $   F   I   L   E
0000260 116 101 115 105 137 127 101 116 124 105 104 042 040 075 040 042
           N   A   M   E   _   W   A   N   T   E   D   "       =       "
0000300 044 106 111 114 105 116 101 115 105 137 103 125 122 122 105 116
           $   F   I   L   E   N   A   M   E   _   C   U   R   R   E   N
0000320 124 042 040 135 073 040 164 150 145 156 015 012 040 040 040 040
           T   "       ]   ;       t   h   e   n  \r  \n
0000340 145 143 150 157 040 042 146 151 154 145 040 146 157 165 156 144
           e   c   h   o       "   f   i   l   e       f   o   u   n   d
0000360 042 015 012 145 154 163 145 015 012 040 040 040 040 145 143 150
           "  \r  \n   e   l   s   e  \r  \n                   e   c   h
0000400 157 040 042 146 151 154 145 040 156 157 164 040 146 157 165 156
           o       "   f   i   l   e       n   o   t       f   o   u   n
0000420 144 042 015 012 146 151 015 012 167 150 151 154 145 040 162 145
           d   "  \r  \n   f   i  \r  \n   w   h   i   l   e       r   e
0000440 141 144 040 114 111 116 105 073 015 012 144 157 015 012 040 040
           a   d       L   I   N   E   ;  \r  \n   d   o  \r  \n
0000460 145 143 150 157 040 042 044 114 111 116 105 042 015 012 144 157
           e   c   h   o       "   $   L   I   N   E   "  \r  \n   d   o
0000500 156 145 040
           n   e
0000503

Vani

The od output clearly shows us that test.sh is not a shell script. Shell scripts are UNIX text files. UNIX text files have a single <newline> character as a line terminator on every line (not the DOS <carriage-return><newline> character pairs as line separators with no line terminator at the end of the file). The <carriage-return> characters in test.sh are extraneous characters at the end of all but the last line in your shell script. They are interpreted as part of the last string on each command line (i.e., as part of a filename operand, as the unknown string then<carriage-return> when the shell keyword then was expected, etc.) and are causing you lots of problems that are sometimes hard to diagnose because the diagnostic messages print the <carriage-return> characters but they frequently are invisible on the screen after they have been printed.

In the shell command language there are single-quotes ( ' ), double-quotes ( " ), and back-quotes ( ` ). Opening and closing double-quotes ( ) and opening and closing single-quotes ( �' ) may look nice in pretty-printed text, but they are syntax errors in a shell script.

Yes, you can use ls -t | head -n1 in a shell script and compare the results to the string saved in the variable FILENAME_WANTED by the command:

FILENAME_WANTED=`date +"HourlyDate_%Y%m%d_%H00.txt"`

but, if instead of the above command you use the command:

FILENAME_WANTED=`date +"HourlyDate_%Y%m%d_%H00.txt"`<CR>

where <CR> is a visual representation of the <carriage-return> character you have in your current version of test.sh , there will never be a match because the string saved in FILENAME_WANTED contains a trailing <carriage-return> character that is not present in the filename you are reading from ls .

If you want to use notepad instead of learning how to use vi , you must configure it to use UNIX line terminators; not DOS line separators and you must configure it to use shell command language single-quotes (not pretty-printed opening an closing single-quotes), double-quotes (not pretty-printed opening and closing double-quotes), and back-quotes (instead of pretty printed single-quotes) or learn to use the highly preferred $(command) form of command substitution instead of the obsolete `command` form of command substitution and stop using back-quotes in your shell scripts.

Hello Don,

I finally used vi editor and got it working ..

# Vani Sonti
# 07/14/2016
# find file name match and load data only when a match found 
# if not display msg
#!/usr/bin/sh
FILENAME_WANTED=`date +"HourlyData_%Y%m%d_%H00.txt"`
echo "FILENAME_WANTED = $FILENAME_WANTED"
LIST_OF_FILES=`ls -rt /tmp/v_tst/HourlyData*.txt |tail -1`
echo "LIST_OF_FILES = $LIST_OF_FILES"
echo "------"
if [ "$FILENAME_WANTED" = "$LIST_OF_FILES" ]; then 
echo "matched"
else echo "NotMatched"
fi

output looks this:

perseus.gasleak(/tmp/v_tst)% sh chk.sh
FILENAME_WANTED = HourlyData_20160714_1200.txt
LIST_OF_FILES = /tmp/v_tst/HourlyData_20160714_1000.txt
------
NotMatched
perseus.gasleak(/tmp/v_tst)%

Everyone, appreciate all your help

Vani