LinuxQuestions.org
Have you listened to LQ Radio?
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
 
Thread Tools
Old 07-05-2006, 11: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: 718
Blog Entries: 7
Thanked: 0
how to change some text of a certain line of a text file with bash and *nix scripting


[Log in to get rid of this advertisement]
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 ...

.
alred is offline     Reply With Quote
Old 07-05-2006, 12:07 PM   #2
jlightner
Senior Member
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 3,548
Thanked: 144
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.
jlightner is offline     Reply With Quote
Old 07-05-2006, 12:34 PM   #3
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 718
Blog Entries: 7
Thanked: 0

Original Poster
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 ??

.
alred is offline     Reply With Quote
Old 07-05-2006, 01:08 PM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: MEPIS, Linux Mint
Posts: 142
Thanked: 3
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.
Hobbletoe is offline     Reply With Quote
Old 07-05-2006, 01: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: 718
Blog Entries: 7
Thanked: 0

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


.

Last edited by alred; 07-05-2006 at 01:36 PM..
alred is offline     Reply With Quote
Old 07-09-2006, 02:38 PM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 377
Thanked: 0
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'/'
eddiebaby1023 is offline     Reply With Quote
Old 07-10-2006, 12:55 PM   #7
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 718
Blog Entries: 7
Thanked: 0

Original Poster
this one also works great ...

strange erhh ... ^_^


.
alred is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 03:44 AM
Bash Scripting help, altering data in text file. trey85stang Linux - General 4 05-31-2006 06:18 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 05:32 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
Change Text-File through bash script arkus Programming 2 12-17-2004 09:48 PM


All times are GMT -5. The time now is 06:52 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration