Results 1 to 10 of 10
I have done some searching, seeing what commands I exactly will need.
I/O redirection
grep I think
echo
and a while loop?
What I want to do is lets say ...
- 02-01-2008 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
Finding and replacing a line in a file
I have done some searching, seeing what commands I exactly will need.
I/O redirection
grep I think
echo
and a while loop?
What I want to do is lets say I have a file with 1000 lines of configuration settings. I want to search the file for a specific line ( I know what the line is ) and once I find this line, I want to replace the line with a new line of code that I give it.
How can this be achieved with linux bash/shell scripting? Do I need something more powerful like perl/python for this task? I would like to avoid those dependencies at all costs, even if it makes it inefficient.
Thanks,
Michael
- 02-01-2008 #2Just Joined!
- Join Date
- Jan 2008
- Posts
- 25
The simplest solution is to use 'sed' like this:
sed -e 's/{line that you want to replace}/{line with which you want to replace}/g'
of course, if your line has special character's then you need to use proper sed syntax which you can easily find by google it or for eg. from here: Sed - An Introduction and Tutorial
There are of course many other ways to do it like using awk, etc.
- 02-01-2008 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
If this 'sed' command doesn't require dependencies (works on most common linux distros) then I'll use it.
My original plan was to use the 'cat' command in a while loop and the 'while read line' and then check Each line that came in, and if the line matched then I would change it. I'll try the sed when I get home!
Thanks.
- 02-01-2008 #4Just Joined!
- Join Date
- Jan 2008
- Posts
- 25
It shouldn't. I would be surprised if any distros comes without sed.
- 02-02-2008 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
They may not all support the -e (edit in-situ) flag, though, in which case you'd have to use a temporary intermediate file.
- 02-02-2008 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 02-02-2008 #7Just Joined!
- Join Date
- Jan 2008
- Posts
- 25
- 02-03-2008 #8Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
No, I did mean the -i flag that the posting didn't actually contain
- the example given requires the use of an intermediate file. I should really read posts properly before I rush in.
- 02-03-2008 #9Just Joined!
- Join Date
- Jan 2008
- Posts
- 25
- 02-04-2008 #10Just Joined!
- Join Date
- Nov 2007
- Posts
- 14
Hrm...
Lets say my file has a line that can look like this:
My Configuration = [ int, int, int, int ]
OR
My Configuration = [ int, int]
Basically there can be any number of integers within the two brackets, but at least one occurrance.
would my sed statement look like:
sed 's/"My Configuration = "\[{int}{",int"}*\]' <mysettings.txt >mysettings.txt
so I'm looking for the My Config. with an [ bracket, then Definitely 1 occurrence of int, with zero or more occurance of ,int after.
Does that make sense?


Reply With Quote
