Read last line of file to check for value

Folks

How best to read the last line of a file, to check for a particular value?

This is the last line of my file......

00000870000002000008 0000000020000

......I need to check that this line contains '70' in positions 7 and 8, before I continue processing.

Regards ... Dave

You can try the below and apply the condition according to the logic of your code :

x='00000870000002000008 0000000020000'
/home/ans >echo $x | awk '{ print substr($0,7,2); }'
70
/home/ans >echo ${x:6:2}
70

tmp=`sed '$!D' File Name | cut -c7-8`
if [ "$tmp" -eq "70" ]
then
<<process the file >>
else
echo "Skip it"
fi

if [ `tail -1 file | cut -c7-8` -eq "70" ] ; then echo "70 found"; fi

The same using awk only

if [ $(awk 'END{print substr($0,7,2)}' file) -eq "70" ] ; then echo "70 found"; fi
tail -1 file | egrep '^.{6}70' >/dev/null && echo Successamundo

I've coded as follows, as I don't know the name of my file (only the prefix), and also there may be mulitple files. I need to check all the files for '70' before I continue processing. I get the error at the bottom:

Code

icofile=`ls -l /Invoices |grep DAAS | awk '{print $9}'`
if [[ $icofile != "" ]]
then
for files in $icofile
do
if [ `tail -1 $files | cut -c7-8` -eq "70" ]
then
echo "File correct format, has record 70..." >>$logfile
else
echo "File incorrect format, missing record 70..." >>$logfile
exit 2
fi
done
echo "Sending $icofile to Server" >>$logfile
else
echo "No files to send across..." >> $logfile
exit 3
fi

Error

"./Transfer.DAAS[25]: test: 0403-004 Specify a parameter with this command."

(where line 25 is my check for '70')

The test command (aka [ -- really) is picky about its syntax. If the output from the backticks is the empty string, it will not see any argument at all where it expects one before the -eq. Either properly double-quote the backticks, or use a construct which is less brittle. Perhaps something like this:

if awk 'FNR == 1 { if (prevfile && prev !~ /^.{6}70/) { print prevfile; rc=1; } }
{ prevfile=FILENAME; prev=
END { if (prevfile && prev !~ /^/.{6}70/) { print prevfile; rc=1 } exit rc; }' $icofile; then
  : ... send to server
fi

Have to admit to not following what you've coded above, so have stuck to the original, trying to "properly double-quote the backticks" - as follows.

for files in $icofile
do
    if [ "tail -1 $icofiles | cut -c7-8" = "70" ] 
    then
        echo "File correct format, has trailer..." &gt;&gt;$logfile
    else
        echo "File incorrect format, missing trailer..." &gt;&gt;$logfile
        exit 2
    fi
done
echo "Sending $icofile to AS400 ODIN" &gt;&gt;$logfile

Even though the file has '70' in positions 7/8, the script is dropping into the first 'else' (missing trailer).

Any ideas?

Try:
VAR1=$(tail -1 $file | cut -c7-8)
#if [ "tail -1 $file | cut -c7-8" = "70" ]
if [ "$VAR1" = "70" ]
then
...

Your loop variable is "file" and the list of files it loops over is $icofile, yet you use $icofiles inside the loop; is this really correct?

You need both double quotes and backticks; you can break it up into a variable assignment with backticks and a comparison of the variable, like vbe suggests, or combine them:

if [ "`tail -1 $file | cut -c7-8`" = "70" ]; then ...

(assuming you really ought to be using $file rather than $icofiles in the command).

Nice point era, I edited and corrrected also... didnt realize the typo change between the last and its previous post of daveaasmith...and I copied/pasted the last...
definitely $file (better than $files which is confusing whe taliking of one record...)

Nice one folks. I have added the missing backticks (didn't realise these were also required, rather than the double quotes replacing them). I had indeed cut/pasted into this thread incorrectly (oops), but am now running the following, successfully:-

for files in $icofile
do
if [ "`tail -1 $files | cut -c7-8`" = "70" ]
then
echo "File correct format, has trailer..." >>$logfile
else
echo "File incorrect format, missing trailer..." >>$logfile
exit 2
fi
done
echo "Sending $icofile to AS400 ODIN" >>$logfile