LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-09-2006, 09:00 AM   #1
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Rep: Reputation: 0
changing line in text file


hi
i am writting a script that needs to manipulate with configuration file, and i don't know how safely change a line in a file, i need to change few chars in that line, but i can also replace it.
so if you know how to do that [so other data in a file is not changed] please post here
some example would be usefull
thanks.
 
Old 02-09-2006, 09:04 AM   #2
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
is it acceptable that you can have the changes in conf.temp
and then rename conf.temp to conf

could you please post sample output, changes expected and the final output conf file
 
Old 02-09-2006, 09:04 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
is it acceptable that you can have the changes in conf.temp
and then rename conf.temp to conf

could you please post sample output, changes expected and the final output conf file
 
Old 02-09-2006, 09:07 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Try using 'sed' to perform the replacement you need:
Code:
sed 's/OLDTEXT/NEWTEXT/g'
 
Old 02-09-2006, 09:10 AM   #5
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
unreadable color sorry

Last edited by tescik80; 02-09-2006 at 09:12 AM.
 
Old 02-09-2006, 09:10 AM   #6
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
#####sample:
useSyslog 1
syslogFacility 0

# auto generation
timer period=3600 nextUpdateBuffer=300 start=00:00:00

other stuff here.....

##############################################################################





file is much bigger, but point is to change start parameter to different full hours.



possible output:


######sample:
useSyslog 1
syslogFacility 0

# auto generation
timer period=3600 nextUpdateBuffer=300 start=17:00:00

other stuff here.....

##############################################################################





it would be ok to rename file
 
Old 02-09-2006, 09:14 AM   #7
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Matir
Try using 'sed' to perform the replacement you need:
Code:
sed 's/OLDTEXT/NEWTEXT/g'
but how to pick the line i need to replace?
i need only 1 line to be changed, i am really newbie...
 
Old 02-09-2006, 01:46 PM   #8
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
if it's line 10:

sed '10 s/OLDTEXT/NEWTEXT/g'
 
Old 02-10-2006, 05:30 AM   #9
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
thx a lot, i will test in in a moment
 
Old 02-10-2006, 11:46 PM   #10
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by sirclif
if it's line 10:

sed '10 s/OLDTEXT/NEWTEXT/g'
you cannot be sure which line needs to be modified,
instead of hard-coding search the pattern and then replace

Code:
sed '/OLDTEXT/ s/OLDTEXT/NEWTEXT/g' file
 
Old 02-12-2006, 09:25 AM   #11
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by kshkid
you cannot be sure which line needs to be modified,
instead of hard-coding search the pattern and then replace

Code:
sed '/OLDTEXT/ s/OLDTEXT/NEWTEXT/g' file
You don't need both the search and substitute patterns, if they're identical.
 
Old 02-12-2006, 12:33 PM   #12
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
can i change a specified fragment of line or just put a new line onstead old one?
that would help a lot, woudn't have to know what param is set.
 
Old 02-12-2006, 01:06 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Why don't you just run the suggested sed command against
your file and see what it does?


Cheers,
Tink
 
Old 02-20-2006, 05:43 AM   #14
tescik80
LQ Newbie
 
Registered: Feb 2006
Posts: 11

Original Poster
Rep: Reputation: 0
can i use maskwith sed?
if i can how would syntax look like?

lets say i awant to change :

i am noob ???????? [8 letters there, which i want to change and i dont know them]

sed s/start=????????/start=$czas/g file1 > file2

what to put there in ????????
 
  


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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Getting a text file line after line in C++ cdog Programming 4 12-31-2005 02:36 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
changing text size on command line anvilmonkey Linux - Software 1 03-21-2005 03:40 PM
View Line In Text File DaveyB Programming 1 11-03-2004 12:02 PM

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

All times are GMT -5. The time now is 07:27 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