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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Edit number of lines in a file to single line chiru_h Shell Programming and Scripting 3 04-01-2008 08:27 PM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 07:19 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-20-2007 10:29 PM
display lines after a particular line number rajashekar.y UNIX for Dummies Questions & Answers 6 01-03-2007 12:53 AM
Update specific lines in a file aukequist Shell Programming and Scripting 3 11-15-2005 10:52 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-07-2008
Registered User
 

Join Date: Jan 2008
Posts: 2
SED: Update Last Line with Number Lines in File

Hi,

I have to update last line of a text file with the number of lines in that file. This last line will have text such as 0.0000 and I should replace this with number lines. If lines are 20 then it should be replaced with 00020. Any sed or awk cmd help would be appreciated
Reply With Quote
Forum Sponsor
  #2  
Old 01-07-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 570
Code:
nawk '{
    if (NR != 1)
       print lstln
    lstln = $0
} END { printf("%05d\n", NR) }' file
Reply With Quote
  #3  
Old 01-07-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Another one:

Code:
awk 'x=$0||!NF;END{printf "%05d\n", NR}'  data
But I believe you don't want to count the last line:

Code:
awk 'x=$0||!NF;END{printf "%05d\n", --NR}'  data

Last edited by radoulov; 01-07-2008 at 03:33 PM. Reason: As always: use nawk or /usr/xpg4/bin/awk on Solaris :)
Reply With Quote
  #4  
Old 01-07-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
with coreutils head command and bash
Code:
numlines=`wc -l file|cut -f1 -d" "`
head -n-1 file > newfile
printf "%05d" $(( numlines-1 )) >> newfile
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:05 AM.


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

Content Relevant URLs by vBSEO 3.2.0