Replace content from a file and save

Hi,
Right now there is a file called 'qm.ini' which is owned by mqm:mqm and I am trying to replace a line from this file with something else and save.

I am using the below perl command to replace and save within a shell script with a different user called 'mqadm' which is also part of mqm group.

perl -i -pe 's/LogBufferPages=0/LogBufferPages=4096/g' qm.ini

======

With the above command, the file will be edited and saved with the user mqadm:mqm.

Question: Is there anyway I can edit a file and save without changing the owner of the file?.

Thanks,

Try:

ed -s qm.ini <<-EOF
	g/LogBufferPages=0/s//LogBufferPages=4096/g
	w
	q
EOF

or:

ex -s qm.ini <<-EOF
	g/LogBufferPages=0/s//LogBufferPages=4096/g
	w
	q
EOF

On some systems, these might also rename the temp file to overwrite your original file (thereby changing the file's owner), but on most systems I think at least ed still does just what you ask it to do (rewrite the contents of the file instead of replace the file).

1 Like