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 a file line by line VENC22 UNIX for Dummies Questions & Answers 4 10-30-2008 11:09 AM
read the file line by line kittusri9 Shell Programming and Scripting 3 04-24-2008 09:26 AM
How to read element of line of file ahjiefreak High Level Programming 1 04-20-2008 03:21 PM
read line from file rein Shell Programming and Scripting 5 04-15-2005 03:32 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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 06-13-2005
yongho yongho is offline
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 01:45 PM..
  #2 (permalink)  
Old 06-13-2005
yongho yongho is offline
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 02:20 PM..
  #3 (permalink)  
Old 06-13-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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
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:08 PM.


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