LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to change some text of a certain line of a text file with bash and *nix scripting (https://www.linuxquestions.org/questions/programming-9/how-to-change-some-text-of-a-certain-line-of-a-text-file-with-bash-and-%2Anix-scripting-461210/)

alred 07-05-2006 10:19 AM

how to change some text of a certain line of a text file with bash and *nix scripting
 
how to change some text of a certain line of a text file with bash and *nix scripting ??

i could have use pascal to do that but ...

how to do it with bash , awk , sed or whatever the *nix guys like to do ??

eg ::
theres a file with the content of ::
Code:

...
private final int invokeThreadK=3;
private final int invokeRunnableK=4;
private final int invokeK_MaxCount=5;
///////////////////////////////////////////////////////////////////////////////////////////////////// 
private final String thisAddr="192.168.1.15";
private boolean terminated = false;
final String bttext="mb01buttton"  ;
...

when i execute a script file something like "dobuild 192.168.1.1"

the above file will be updated to ::
Code:

...
private final int invokeThreadK=3;
private final int invokeRunnableK=4;
private final int invokeK_MaxCount=5;
///////////////////////////////////////////////////////////////////////////////////////////////////// 
private final String thisAddr="192.168.1.1";
private boolean terminated = false;
final String bttext="mb01buttton"  ;
...


//thanks in advance ...

.

MensaWater 07-05-2006 11:07 AM

Code:

sed s/192.168.1.15/192.168.1.1/ inputfile >outputfile
The above will find any occurrence of 192.168.1.15 in input file and replace it with 192.168.1.1 and write all lines into outputfile. You can then

Code:

mv inputfile inputfile.DATESTAMP
mv outputfile inputfile

This will save a copy of your original inputfile before overwriting it with what you changed in the outputfile.

alred 07-05-2006 11:34 AM

let me catch some breath first ...

...

ok ...

great and fast , thanks ...

i believe your code works but what if i want to "search" for the occurance of private final String thisAddr= and change the remaining text until the end of line ...

probably i should give a "dobuild "192.168.1.1"" or something ??

what should i do ??

.

Hobbletoe 07-05-2006 12:08 PM

Try as your "dobuild" script ...
Code:

#!/bin/bash

TEMP=$(mktemp /tmp/tmpxxxx)
sed 's/\(private final String thisAddr=\)\(.\)*/\1'$1'/' file_name > $TEMP
mv $TEMP/file_name

exit

Substituting the name of your file instead of file_name, obviously.

alred 07-05-2006 12:33 PM

thank to both of you and the hints on doing things more carefully ... they work great ...


.

eddiebaby1023 07-09-2006 01:38 PM

Quote:

Originally Posted by Hobbletoe
Try as your "dobuild" script ...
Code:

sed 's/\(private final String thisAddr=\)\(.\)*/\1'$1'/'

Waste of typing saving the original IP address:
Code:

sed 's/\(private final String thisAddr=\).*/\1'$1'/'

alred 07-10-2006 11:55 AM

this one also works great ...

strange erhh ... ^_^


.


All times are GMT -5. The time now is 01:04 PM.