Script to delete few rows from a file and then update header

HJKL1Name00014300010800000418828124201
L201207022012070228XAM  00000000031795404  001372339540000000000000000000000                                   COOLTV KEYA
    Zx00        xI-50352202553           00000000
                        00000000    G000000000000           00000000  2160101003
L000000000000000028     00000000040        001372339540000000000000000000000   MSRCO2
                                         000000000
                        00000000     G000000000000           00000000  2160101003

I have file with 108 lines in it like above (2 lines) this is having COOLTV , they have other names
I want to open file and remove line which has COOLTV mentioned init and also want to delete immediate next line. these two lines can be any where in the file.

then once it delete in the header it should also update 108 as 106 because we delete 2 lines and it should change 00000418828124201 to 00000514443225 (00000418828124201 - 00137233954 = 00000514443225)

Header after modify should look like

HJKL1Name0001430001060000051444322201

it should keep orginal file there and should make new file will all these changes

106 will be word count and 0000051444322 is remaining after subtracting 00137233954
how can we do it using awk or shell

awk ' NR == 1 {
                HF = substr($0,1,15);
                HC = substr($0,16,6);
                HR = substr($0,22);
} /COOLTV/ {
                R  = $3;
                sub(/0+$/,x,R);
                $0 = ""; getline; $0 = "";
                HC -= 2;
                HR -= R;
} !/COOLTV/ {
                print $0 > "newfile";
} END {
                printf "%s%06d%017d", HF, HC, HR > "header";
} ' file

HDR=$( cat header )

awk -v H="$HDR" 'NR==1{ sub($0,H) }1' newfile

Note: Use nawk for SunOS or Solaris

removed

Try this modified code. Make necessary adjustments as per your requirement:

awk ' NR == 1 {
                HF = substr($0,1,15);
                HC = substr($0,16,6);
                HR = substr($0,22,14);
                HE = substr($0,36);
} /COOLTV/ {
                R  = $3;
                sub(/0+$/,x,R);
                HC -= 2;
                HR -= R;
                getline; next;
} !/COOLTV/ {
                print $0 > "newfile";
} END {
                printf "%s%06d%014d%d", HF, HC, HR, HE > "header";
} ' file

HDR=$( cat header )

awk -v H="$HDR" 'NR==1{ sub($0,H) }1' newfile

Hello Bipinajith
The Header file which gets created is not replacing header in newfile

when I tried separately i saw it printed value but when I did cat of file it was not appearing there

[wasim]$ HDR=$( cat header )
[wasim]$ echo $HDR
HJKL1Name00014200011800000463190664201
[wasim]$ nawk -v H="$HDR" 'NR==1{ sub($0,H) }1' check.txt
HJKL1Name00014200011800000463190664201
[wasim]$ cat check.txt
HJKL1Name00014200012000000600424618201                                                                                                                                                                                                                                                                                    

It works for me with the sample content you posted!

You can debug the code by putting print statements in it and see the values computed for each records processed.

[quote=bipinajith;302771351]
It works for me with the sample content you posted!

Because awk does not modify the content of your file.

Redirect output to a temp file and rename it to original:

awk -v H="$HDR" 'NR==1{ sub($0,H) }1' newfile > tmp
mv tmp newfile

I hope this helps.

Okay I will check

No, it should be:

HE = substr($0,36,3);
HS = substr($0,39,421);

removed