Sed text replacement issue.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed text replacement issue.
# 1  
Old 11-25-2008
Sed text replacement issue.

Hi,
Im trying to find and replace text within a unix file using sed.
The command that i have been using is

sed '/,null,/ s//, ,/g' result.txt>result.tmp

for replacing ",null," with ", ,".
But this only replaces the first occurrance of ,null, in every line. I want to do it globally.
It works fine if i exclude all the commas.
Please help.
# 2  
Old 11-25-2008
Should be something like:

Code:
sed 's/,null,/, ,/g' result.txt > result.tmp

# 3  
Old 11-25-2008
Quote:
Originally Posted by sohaibs
Hi,
Im trying to find and replace text within a unix file using sed.
The command that i have been using is

sed '/,null,/ s//, ,/g' result.txt>result.tmp

for replacing ",null," with ", ,".
But this only replaces the first occurrance of ,null, in every line. I want to do it globally.
It works fine if i exclude all the commas.
Please help.
Doesn't this work ?

Code:
sed -e "s/,null,/, ,/g" result.txt > result.tmp

# 4  
Old 11-25-2008
sed -e "s/,null,/, ,/g" result.txt > result.tmp
sed 's/,null,/, ,/g' result.txt > result.tmp

Both the above statements only replace the first occurance of ,null, to , , in each line.
Could this be done in any other way? Im trying to modify the contents of a file using a shell script that first extracts this data and modifies it a bit.
# 5  
Old 11-25-2008
It should work, post your file within code brackets (select the code and click on the "#" above the editing window).

Regards
# 6  
Old 11-25-2008
Code:
2,520,DB_MSC_MIT,null,KBNY,BLAH,US,5876548,VBU3M.U,VZ0M.U,U,VZFSK,BK,0.772084,ER,10.0,1.0,A,P,1.0,0.0,2009-01-16T00:00:00 @Europe/London,0.0,35.0,To La,1.0,1.6936391,7.07878526,6.629309,242.30009,Amer,1.3309,0.0,0.0,0.0,922.808018,312.3315240926514338,29.83157600000000,574,37.53658120375646,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-26.10513536513008,0.0,-74951.16306028693,-26.10513536513008,-74925.0579249218,0.0,0.0,0.0,0.0,0.0,0.0,-237.22390960441317,0.0,4896.289228465125,-237.22390960441317,5133.513138069538,0.0,0.0,0.0,0.0,0.0,0.0,-4755.8670072606055,0.0,458181.9339556703,-4755.8670072606055,462937.8009629309,null,null,null,0.0,O,VZFRP JAN 09 35 P,null,null,1.0,null,VP35 1.0,null,null,null,O,BSK,BSK,null,null,DrFr,NY,NY,null,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2,0.0,0.0,0.0

# 7  
Old 11-25-2008
Both ways are working on my boxes... Though maybe try something like (even I think it is unnecessary):
Code:
sed -e :a -e 's/,null,/, ,/;ta' result.txt

Edit:
Just saw the long example line.
With the shown line the 2 sed commands do not work on my boxes - I guess because of the already substituted adjacent commas of following ,null,null. So the loop in sed will be ok.

Last edited by zaxxon; 11-25-2008 at 07:15 AM.. Reason: Added info
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed text replacement

Hello, I'm using Bash and Sed to replace text within a text file (1.txt) twice in one script. Using a for loop I'm initially replacing any 'apple' words with the variable 'word1' ("leg). I'm then using another for loop to replace any 'apple' words with the variable 'word2' ("arm"). This task is... (2 Replies)
Discussion started by: Flip-Flop
2 Replies

2. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Text replacement with awk or sed?

Hi guys, I worked for almost a half-day for the replacement of some text automatically with script. But no success. The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong. For example, I... (2 Replies)
Discussion started by: liuzhencc
2 Replies

4. Shell Programming and Scripting

Block of text replacement using sed

Hi, I have a requirement in which i need to replace text as below - <stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt; &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt; &lt;Param Name=&quot;Value&quot;... (0 Replies)
Discussion started by: abhitanshu
0 Replies

5. Shell Programming and Scripting

SED replacement

Hi, i have a file with lines, file.txt ------- test is fun testing is better I need to replace 'test' to 'develop' and i used, a=test b=develop sed "s,$a,$b,g" -------- but i see the word 'testing' is also replaced. Need some solution. Is there any way i could replace only 'test' ? (4 Replies)
Discussion started by: giri_luck
4 Replies

6. Shell Programming and Scripting

Replacement with sed

I am trying to replace the line which has string "tablespace" not case senstive.... with below simple script: mysrcipt.sh sed "s/.*/TABLESPACE USERS/g" create_table > tmp mv tmp create_table Is there any better way to do it? If Search string tooooooo long it will be tough to code in... (4 Replies)
Discussion started by: ganeshd
4 Replies

7. Shell Programming and Scripting

Help with sed replacement

This seems like it should be an easy problem, but I'm a noob and I can't figure it out. I'm trying to use sed, but would be happy to use anything that does the job. I am trying to trim off a fixed number of unknown characters from 2 different : delimited fields while keeping the intervening... (4 Replies)
Discussion started by: helix_w
4 Replies

8. Shell Programming and Scripting

Need Replacement for sed

Hi Can anyone provide me the replacement of sed with xargs perl syntax for the below sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' This should be without looping has to take minimal time for search (0 Replies)
Discussion started by: dbsurf
0 Replies

9. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies
Login or Register to Ask a Question