Replace content from a file and save


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace content from a file and save
# 1  
Old 03-07-2016
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.
======
Code:
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,
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 03-07-2016 at 04:16 PM.. Reason: Add CODE tags.
# 2  
Old 03-07-2016
Try:
Code:
ed -s qm.ini <<-EOF
	g/LogBufferPages=0/s//LogBufferPages=4096/g
	w
	q
EOF

or:
Code:
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).
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

2. Shell Programming and Scripting

How to save sorted content of a inside the same file?

Hi All, When i use sort Test, here is the output: $ sort Test a b b c d e f g h i But the contents in the file remain unsorted, how to do that? ]$ cat Test g i (6 Replies)
Discussion started by: chandrakanth
6 Replies

3. Shell Programming and Scripting

How to replace multiple file content?

Hi Gurus, I need replace multiple files content. the file name pattern likes currentfile_code_* the content pattern in the file like text=value I need replace the content as text=abcde Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

4. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. Shell Programming and Scripting

Need to replace particular content in a xml file

Hi, My requirement is to find a text and replace it with another in a XML file. I am new to Unix,Please provide some suggestion to achieve. Find: <Style ss:ID="ColumnHeader1"> Replace with: <Style ss:ID="ColumnHeader1"> <Borders> <Border ss:Position="Bottom"... (4 Replies)
Discussion started by: cnraja
4 Replies

6. Shell Programming and Scripting

Replace string by file content

hi I have template file my.tpl: bla-bla-bla <link href="style.css" type="text/css"> bla-bla-bla and style.css : body{margin: 0px;} I want to get in result one file: bla-bla-bla <script>body{margin: 0px;}</script> bla-bla-bla I tryed to used SED: sed '/<link .*href=\"(*)*\"... (6 Replies)
Discussion started by: dim_nsk
6 Replies

7. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

8. Shell Programming and Scripting

How to replace some content of a file and write with a new name

Hi I have a control file which looks like this LOAD DATA INFILE '/home/scott/XXX.dat' PRESERVE BLANKS ............. ............. how can i change the content of this file and replace the file in the second line with anothe file name and write it back with another name to the disk? ... (5 Replies)
Discussion started by: mwrg
5 Replies

9. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies

10. Shell Programming and Scripting

replace a string with content from another file

Hi, I'm a newbi in shell script. Here what I want to do: FileA: bor bor bor xxxx bib bib bi FileB: something something something I want to replace string "xxxx" in FileA with contents of FileB. i tried with sed: fileb=`cat FileB` reg=xxxx file=FileA (4 Replies)
Discussion started by: afatguy
4 Replies
Login or Register to Ask a Question