0

I have *.txt file with Header and Trailer. Trailer contains number of specific records. I am performing some file manipulation where I am removing certain records and I need to adjust Trailer record count.

I have command that does that but this command changes record length when numbers adjusted from bigger to smaller or vise versa. Here is example of the data.

HDRFILENAME   SOMETHING SOMETHING        
H ICOCOM   SOME DATA1                    
H ICOCOM   SOME DATA2                    
H ICOCOM   SOME DATA3                    
H ICOCOM   SOME DATA5                    
H ICOCOM   SOME DATA6                    
H ICOCOM   SOME DATA7                    
H ICOCOM   SOME DATA8                    
H ICOCOM   SOME DATA9                    
TLR11                                     

I need trailer record adjusted and output look like this:

1.......10........20........30........40..
HDRFILENAME   SOMETHING SOMETHING        
H ICOCOM   SOME DATA1                    
H ICOCOM   SOME DATA2                    
H ICOCOM   SOME DATA3                    
H ICOCOM   SOME DATA5                    
H ICOCOM   SOME DATA6                    
H ICOCOM   SOME DATA7                    
H ICOCOM   SOME DATA8                    
H ICOCOM   SOME DATA9                    
TLR9                                      

File record length is 42 characters, and should stay same way after trailer count adjusted.

I use the following command to changed trailer record count:

{awk '/H ICOC/{cnt++} {sub(/TLR[0-9]+/,"TLR"(cnt+0))} 1' file}

Can someone help on how I can keep/maintain the same record length?

enter image description here

enter image description here

$ cat -Ev test_mark.txt

HDRFILENAME   SOMETHING SOMETHING        ^M$
H ICOCOM   SOME DATA1                    ^M$
H ICOCOM   SOME DATA2                    ^M$
H ICOCOM   SOME DATA3                    ^M$
H ICOCOM   SOME DATA4                    ^M$
H ICOCOM   SOME DATA5                    ^M$
H ICOCOM   SOME DATA6                    ^M$
H ICOCOM   SOME DATA7                    ^M$
H ICOCOM   SOME DATA8                    ^M$
H ICOCOM   SOME DATA9                    ^M$
TLR11                                    ^M$
$

$ awk '/H ICOC/{cnt++} /^TLR/{$0=sprintf("%-*s", length(), "TLR"(cnt+0))} 1' test_mark.txt | cat -Ev

HDRFILENAME   SOMETHING SOMETHING        ^M$
H ICOCOM   SOME DATA1                    ^M$
H ICOCOM   SOME DATA2                    ^M$
H ICOCOM   SOME DATA3                    ^M$
H ICOCOM   SOME DATA4                    ^M$
H ICOCOM   SOME DATA5                    ^M$
H ICOCOM   SOME DATA6                    ^M$
H ICOCOM   SOME DATA7                    ^M$
H ICOCOM   SOME DATA8                    ^M$
H ICOCOM   SOME DATA9                    ^M$
TLR9                                      $
3
  • So do you want to zero pad the 9 - or print the whole TLR9 left-justified in a fixed field width? – steeldriver Mar 2 '20 at 20:28
  • When you say Record length is 42 bytes - is a "record" to you your whole input file or part of it (if so which part?) or is it a single line? Why are you talking about "bytes" instead of "characters"? If you're trying to say "the last line of my input file is 42 characters long and I need it to stay that length" then why not just say that? If you mean something else - what? – Ed Morton Mar 2 '20 at 21:47
  • Hi, each record on the file is 42 characters long, After running awk command you gave me, it did adjust trailer count but added extra character to that record and made it 43 character long. Again thank you for your help. – mamtsism Mar 5 '20 at 14:14
2

If you just want the "TLR" line of your input to be the same length after processing as before then that'd be:

awk '/H ICOC/{cnt++} /^TLR/{$0=sprintf("%-*s", length(), "TLR"(cnt+0))} 1'

and the final solution given we now know you have DOS line endings and want to retain them:

awk -v ORS='\r\n' '{sub(/\r$/,"")} /H ICOC/{cnt++} /^TLR/{$0=sprintf("%-*s", length(), "TLR"(cnt+0))} 1' file
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. – terdon Mar 5 '20 at 17:25
0
awk '/H ICOC/{cnt++} /^TLR/ {$0 = sprintf("TLR%-39d", cnt)} 1' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.