Command to update last line.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command to update last line.
# 1  
Old 08-09-2011
Command to update last line.

Hi,
I need to replace the record count which is in the last line of the text file to a new record count.

The file is a fixed length file, and whole file is list of numbers and the last line looks like this.

9#EOF# 00000000000000000016 00000 0000000000

The character between 12 to 30 represents the record count excluding the header and footer. I need to change the count in the last line.

ex 00000000000000000016 in the above needs to be replaced by 00000000000000000036
The characters "9#EOF# " is fixed.

Please suggest. Perl or sed or awk is fine.
Thanks.

Last edited by shinny; 08-09-2011 at 08:30 AM..
# 2  
Old 08-09-2011
If I understood correct:
Code:
$> cat infile
header
eins
zwei
drei
9#EOF# 00000000000000000016 00000 0000000000
$> awk '{l[x++]=$0} END{for(z=0; z<x-1; z++){print l[z]}; $2=sprintf("%020d", NR-2); print}' infile
header
eins
zwei
drei
9#EOF# 00000000000000000003 00000 0000000000

# 3  
Old 08-10-2011
Thanks for the above reply, would you pls explain me this logic. I missed mentioning that the input file contains only numbers and the file would be having 10 to 15 millions of records. So traversing like this will it be fine.
# 4  
Old 08-10-2011
Sure:

{l[x++]=$0} _________________ Store every line ($0) into the array "l" (small L) where x is it's element's index and increment it every line by 1 (++).
END ________________________ When all lines are processed, we are here, so do the following:
for(z=0; z<x-1; z++) __________ A for-loop counting up from zero to the number of lines which is equal to x, but subract 1, so we pass the last element, since this will be treated now. Automatically in the END{} part, $0 is equal to the last line of those been processed.
{print l[z]} __________________ First we print out all elements but the last of the array, so you'll see all lines but the last.
$2=sprintf("%020d", NR-2) _____ Currently $2 of $0 ie. field 2 of the current (last) line has the value we want to change. So we alter field 2 by printing out the number of lines -2 (lines in awk are called Records and N stands for number, so NR means Number of Records) because we want to subtract the header and the footer line.
print _______________________ After modifying field 2 ($2) we just print out the whole (new) line.


If the input lines contain only numbers and sowith is completely differently structured than the example you gave, then this code will most probably not work.
You should post a realistic example then of course.
# 5  
Old 08-10-2011
Hi Zaxxon,
Thanks for the explanation.
the input file looks like this.

Header20110729 02:36
11100000034ATC 00000 MGR0000000000 05177555633051775556IIGRN 0386473260HTR06019960802 JGXA8319991102C874277 HT100019981112C967021 1994101119890813IQ01122 CPS D064134 00010101038647326020040204D064134
11100000065ABC 00000 0000000000 00842891166008428911K9DRN 268440207 959K9D20000720FCP2BU08959VAI19980331IV04279 90000119940530 1970010119700101CPSBATCHFLEX C820377 XXXX00010101345811 19951125
9#EOF# 0000000000000501168


I am little confused with the explanation:
"$2=sprintf("%020d", NR-2) _____ Currently $2 of $0 ie. field 2 of the current (last) line has the value we want to change. So we alter field 2 by printing out the number of lines -2 (lines in awk are called Records and N stands for number, so NR means Number of Records) because we want to subtract the header and the footer line"

I want to modify the last line itself.. so is it going to do the last line or last but one ?

thanks in advance.
# 6  
Old 08-10-2011
As in the new excerpt $2 has 19 characters instead of 20, try this:
Code:
awk '{l[x++]=$0} END{for(z=0; z<x-1; z++){print l[z]}; $2=sprintf("%019d", NR-2); print}' infile

Since $2 is still the field which has or should have the number of lines minus header and footer in it, it will work with just this change. The now missing $3 and $4 of the former example is no problem.

It only modifies the last line, ie. field 2 ($2) of the last line and then prints out the modified line.
# 7  
Old 08-10-2011
Should memory become an issue at some point, here's a variation of zaxxon's most recent solution that instead of storing all records in memory only stores one:
Code:
awk 'NR>1 {print prev} {prev=$0} END {$2=sprintf("%019d", NR-2); print}' infile

Regards,
Alister
This User Gave Thanks to alister For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use awk to update only 2nd line

I have a file and need to update the specific position on line two. I tried to use this solution under the post "Awk command to replace specific position characters" But need a way to just limit to update the line 2 and not every line on the file. awk 'function repl(s,f,t,v) { return... (1 Reply)
Discussion started by: Sandeep
1 Replies

2. Shell Programming and Scripting

Find and update line in xml file

Hi, I have a xml file that I need to modify 1 line to change some value from 2 to 10 (or any number). Sample input: <!-- some text here> . . . <message:test name="ryan"> <message:sample-channel charset="UTF-8" max-value="2" wait="20"> ... (5 Replies)
Discussion started by: brichigo
5 Replies

3. Shell Programming and Scripting

sed insert new line does not update file

Hi all, I have a file called "text.cpp" with the first line of "1" afterwards I tried in Ubuntu to type the following sed '12iasdasdasdasdsad' test.cpp > output.txt however when I tried to see the result of output.txt #cat output.txt 1 why is the line 12 is not updated to the... (6 Replies)
Discussion started by: peuceul
6 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

Update single line

Hi everyone i need one help don't know whether it is simple or difficult but not able to solve it. here is the problem suppose my code is time_def=3 r=0 while ] do echo "time left is $time_def " ((time_def=time_def-1)) done and the output is time left is 3 time left... (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

7. Shell Programming and Scripting

How to find spaces and update values on the same line.

Dear all, I'm trying to write a script where: A file contains more or less 2000 lines. To some of those lines, in a specific position, let's say 89-92 there are spaces. So, this script should find these spaces on specific position and update a value (from 2 to 1) to another position of the... (4 Replies)
Discussion started by: paniklas
4 Replies

8. UNIX for Dummies Questions & Answers

Python update already printed line.

Hi. I have a basic script in python that outputs like this.. $ ./test.py 1% 2% 3% 4% 5% 6% 7% 8% 9% 10% ... But how can I make it so the output stays in 1 line? So it would look something like this.. $ ./test.py 10% (1 Reply)
Discussion started by: cbreiny
1 Replies

9. UNIX for Dummies Questions & Answers

Update a particular line in a file

Hii to all.. I have a file stud.lst bash-3.1$ cat stud.lst NO NMAE DOB 10 +2 BE AVG 075 syam saksena 12/12/55 500 398 550 48.26 099 sachin 11/05/47 450 500 600 51.66 300 mohan kumar 19/04/43 500 600 700 60.00 100 john 12/12/52 800 750 700 75.00 125... (2 Replies)
Discussion started by: krishnampkkm
2 Replies
Login or Register to Ask a Question