Overwrite & Delete in Text File

Dear All,

I have text file like this:

Header
Record 1
Record 2
.......
Record n
Tail

This line of code :
awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename

converted Header and Trailer in one line and within 300 character each.The output is just one line.

Now I want to overwrite this one line above (Header+ Tail) over the first line of original file (orginal header) and delete the Tail........i can copy this header and tail to new file and then append Record 1 to Record n to this new file, but that will cause too low performance........so I think better to update first and the last line of the this file.................so how can i do that?

i.e. 1-update the header with new line i.e. (header+trailer)
2-delete the tail in original file (may be using cut tail -1 filename?)

Dont bump up your issue by opening a new thread. You can address it in your old thread

Header and Footer

Save the output from the awk into a variable like

New_head=$(awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename )

And then you can do something like

sed -e "1 c\ $New_head" -e '$d' filename

Thanks.....I tried the code but sed command is not working properly,it is giving output:

sed: command garbled: 1 c\ Header is: Headervalue.......Trailer is: Trailer value.....

It makes no change in the file as well.

any luck for me as well?

There has been another thread as lorcan said and this seems to be only a continuation of the above thread in a new post.

Please continue to post your question in the same thread so that it would be easier to follow up rather than starting a new thread for the same question.

Assuming I have understood your requirement try this,

awk ' { if ( NR == 1 ) { head=$0 } else { last = $0 } arr[i++]=$0; }END{ printf "Header is : %s Trailer is : %s\n", head, last; for( x=0; x<i-1; x++) { print arr[x] } }' filename
Header is : header Trailer is : trailer
header
record 1
record 2
record 3

combined header and trailer is available followed by that
header is available
all the records are displayed
trailer record is not displayed

If this is not what you had asked for, let us know :slight_smile:

To print the Header and trailer in one line with 300 char each. Removal of header and trailer line. Use the below

Just a flavour to matrixmadhan's command as per your req

awk ' { if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);} arr[i++]=$0; }END{ printf "Header is : %-300s Trailer is : %-300s\n", head, last; for( x=1; x<i-1; x++) { print arr[x] } }' filename

thanks for the reply......the code is working fine.....ur code is :

awk ' { if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);} arr[i\+\+]=$0; }END{ printf "Header is : %-300s Trailer is : %-300s\n", head, last; for( x=1; x<i-1; x++) { print arr[x] } }' old_file > new_file

The problem remains still the same ......for example if there are 1000 rows, one header and one trailer.......it concatenates hearder and trailer in one line and then copies this along with other 1000 rows to new file.......thus cuasing a lot of I/O........i have millions of records in the file and cannot afford to transfer one million records just for the sake of 2 lines (i.e. header and trailer)................

Before this, i got this code:

awk ' { if ( NR == 1 ) { head=$0 } else { last = $0 } }END{ printf "%-300s %-300s\n", head, last }' old_file > new_file
sed '1d;$d' old_file >> new_file

the above code has the same problem that it appends all the data rows................pls note that output is coming fine in both cases but I am concerned about the performance................all we need to do is:

1-Get header in old file (say string1)
2-Get trailer in old fine (say string2)
3-concatenate string1 and string2 (say the result is string3)
4-update the header in old file with string3
5-delete the trailer in old file.

Thanks....

First command with just awk logically should take lesser time than the second one with both awk and sed as the file is parsed twice in the latter case.

ok that is fine but I was just concerned about the number of files created........there is one problem that I am getting one extra line (blank line) at the end of file...........why is so using this code:

awk ' { if ( NR == 1 ) { head=substr($0,1,149);} else { last = substr($0,1,149);} arr[i\+\+]=$0; }END{ printf "%-149s %-149s\n", head, last; for( x=1; x<i-1; x++) { print arr[x] } }' file1>file2

When I open file2,I get one extra line.......?

This works fine for me.
is there any blank space between trailer and detailed record ?

The output I get is like this:

Header (plus) Trailer
Record1
Recrod2
...
...
Record n
(blank line here)

Everything is perfect except that why this blank line is appearing when we have removed the trailer in original file?I want the last line Record n.

The original file was like this:

Header
Record1
Recrod2
...
...
Record n
Trailer