Results 1 to 8 of 8
Hi Guys,
I have a document, which contains a series of numbers such as:
0
21
551
1
1
2
1
1
5
0
Now, i would like to find ...
- 05-10-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 18
Replace command
Hi Guys,
I have a document, which contains a series of numbers such as:
0
21
551
1
1
2
1
1
5
0
Now, i would like to find a method of parsing the entire document, and replacing any 0s with 1s, therefore the example above would become:
1
21
551
1
1
2
1
1
5
1
Any idea of a command to achieve this?
Thanks!
- 05-10-2008 #2
Read up on "sed"
Men occasionally stumble over the truth,
but most of them pick themselves up
and hurry off as if nothing had happened.
Winston Churchill
... then the Unix-Gods created "man" ...
- 05-10-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 18
Thanks... that did the trick:
sed 's/string1/string2/g'
Cheerz!
- 05-11-2008 #4Just Joined!
- Join Date
- May 2008
- Posts
- 18
Hi once again, hit a bump here...
If you run the following:
sed s/0/1/g yourfile > newfile
If the number has a 10, this will change to 11... :S
I need to change 0 to 1, only if it is a whole 0 not 10, 100, 101 etc....
I tried sed s/"0"/1/g, however no luck...
Is there any way around this???
thanks!
- 05-11-2008 #5Just Joined!
- Join Date
- May 2008
- Posts
- 2
the same problem
~~~spot closely...
- 05-11-2008 #6Just Joined!
- Join Date
- May 2008
- Posts
- 18
You'll have to put the sed argument in double quotes or it will interpret the $ as the start of a variable:
sed -e "s/^0$/1/"
However, for some strange reason, it doesn't work in a script.... only when executed directly...
Can you check it out and let me know if you get the same results??
Thanks!
- 05-11-2008 #7Linux User
- Join Date
- Jun 2007
- Posts
- 318
Actually you have to use single quotes otherwise the $ will be interpeted. Although the way it's used in the sed command in this case it doesn't seem to matter. I tried it in a script and it worked for me.
Code:#!/bin/bash -vx echo "0"|sed -e "s/^0$/1/" + echo 0 + sed -e 's/^0$/1/' 1 exit + exit
- 05-12-2008 #8Just Joined!
- Join Date
- May 2008
- Posts
- 18
Thanks m8!! That did it!


Reply With Quote