LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2006, 10:19 AM   #1
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
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 ...

.
 
Old 07-05-2006, 11:07 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 07-05-2006, 11:34 AM   #3
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658

Original Poster
Blog Entries: 8

Rep: Reputation: 31
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 ??

.
 
Old 07-05-2006, 12:08 PM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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.
 
Old 07-05-2006, 12:33 PM   #5
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658

Original Poster
Blog Entries: 8

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


.

Last edited by alred; 07-05-2006 at 12:36 PM.
 
Old 07-09-2006, 01:38 PM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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'/'
 
Old 07-10-2006, 11:55 AM   #7
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658

Original Poster
Blog Entries: 8

Rep: Reputation: 31
this one also works great ...

strange erhh ... ^_^


.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM
Bash Scripting help, altering data in text file. trey85stang Linux - General 4 05-31-2006 05:18 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration