The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read the file line by line kittusri9 Shell Programming and Scripting 3 04-24-2008 05:26 AM
How to read element of line of file ahjiefreak High Level Programming 1 04-20-2008 11:21 AM
Read a file line by line VENC22 UNIX for Dummies Questions & Answers 2 05-12-2005 10:13 AM
read line from file rein Shell Programming and Scripting 5 04-14-2005 11:32 PM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 05:47 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
How to read last line of a txt file?

I need to read the last file for a particular day, such as, "Jun 13" because the CSV file is cumulative for the entire day, so I don't want all the previous files, I just want the last file, for that day.

I ran an 'ls -al | grep "June 13" > myLs.txt' (simplified) to list all files from that day.

So.. instead of using a bunch of greps and cuts and then expr comparisons to find the last file for that day based on date/time.
I'm just assuming that the last line of each myLs.txt will be the last file for that day.

Does awk have a "last line" variable?
I was looking for it and I only found ones for number of "fields per record" and "current record number", but no "number of records total".

Hmm... I guess it would be better if i just, wc -l the textfile and then redirect that into a variable which I will then use in my awk command to echo the last line..... That would work wouldn't it.. hmm..

Last edited by yongho; 06-13-2005 at 09:45 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-13-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
hrmm

firstVar=`wc -l < ls.txt` # get wc -l
firstVar=`echo $firstVar| tr -d ' '` # remove extra spaces
echo "firstVar:${firstVar}" # echo test to see if var worked
secondVar=`awk "END {print $$firstVar}" ls.txt` # awk to print last line of file

Hm... I think I'm complicating things.
I'm getting closer.

ah hah! There's a tail command.

Last edited by yongho; 06-13-2005 at 10:20 AM.
Reply With Quote
  #3 (permalink)  
Old 06-13-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by yongho
firstVar=`wc -l < ls.txt` # get wc -l
firstVar=`echo $firstVar| tr -d ' '` # remove extra spaces
echo "firstVar:${firstVar}" # echo test to see if var worked
secondVar=`awk "END {print $$firstVar}" ls.txt` # awk to print last line of file

Hm... I think I'm complicating things.
# print the last line of a file (emulates "tail -1")
sed '$!d' # method 1
sed -n '$p' # method 2

# print first line of file (emulates "head -1")
sed q

sed1liners
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0