![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
nawk '{
if (NR != 1)
print lstln
lstln = $0
} END { printf("%05d\n", NR) }' file
|
|
#3
|
||||
|
||||
|
Another one:
Code:
awk 'x=$0||!NF;END{printf "%05d\n", NR}' data
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 :) |
|
#4
|
|||
|
|||
|
with coreutils head command and bash
Code:
numlines=`wc -l file|cut -f1 -d" "` head -n-1 file > newfile printf "%05d" $(( numlines-1 )) >> newfile |
|||
| Google The UNIX and Linux Forums |