The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read a file line by line VENC22 UNIX for Dummies Questions & Answers 4 10-30-2008 11:09 AM
read a file line by line in ksh chella Shell Programming and Scripting 3 08-29-2008 02:57 AM
read the file line by line kittusri9 Shell Programming and Scripting 3 04-24-2008 09:26 AM
Read string from a file,plz help me to check joshuaduan Shell Programming and Scripting 1 05-11-2007 04:12 AM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 09:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-04-2008
daveaasmith daveaasmith is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 13
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
  #2 (permalink)  
Old 09-04-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 610
Quote:
Originally Posted by daveaasmith View Post
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 :


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

  #3 (permalink)  
Old 09-04-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87
tmp=`sed '$!D' File Name | cut -c7-8`
if [ "$tmp" -eq "70" ]
then
<<process the file >>
else
echo "Skip it"
fi
  #4 (permalink)  
Old 09-04-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555

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

  #5 (permalink)  
Old 09-04-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,438
The same using awk only

Code:
if [ $(awk 'END{print substr($0,7,2)}' file) -eq "70" ] ; then echo "70 found"; fi

  #6 (permalink)  
Old 09-04-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652

Code:
tail -1 file | egrep '^.{6}70' >/dev/null && echo Successamundo

  #7 (permalink)  
Old 09-05-2008
daveaasmith daveaasmith is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 13
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')
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0