sed text replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed text replacement
# 1  
Old 03-15-2017
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 dependent on the text file 'resetting' it's contents back to it's original form prior to the second for loop; otherwise the second text changes cannot be made. My issue is, the text file does not reset; within the script it keeps the new text replacements.

Code:
#!/bin/sh

word1="leg"
word2="arm"

for i in '1.txt';
do
	sed -e 's@apple@'$word1'@g' $i;
done

cat 1.txt

for i in '1.txt';
do
	sed -e 's@apple@'$word2'@g' $i;
done

Is there any way to reset the contents of the text file back to it's original content during a script? I am aware that the argument '-e' only temporarily changes a file's contents, but the file's contents only reset when a script has finished.

With a text file containing only one word 'apple', I expect the above script to replace the the word 'apple' with the word 'arm'. Only, for reasons described above, it is currently only changing to 'leg'.

Thanks,
F
# 2  
Old 03-16-2017
What is your intention? First changing to leg then to arm does not make sense: you can directly change to arm.
Please give an example input and expected output.
# 3  
Old 03-16-2017
Quote:
Originally Posted by Flip-Flop
.
.
.
I'm using Bash
Do you? Your script's "shebang" tells otherwise:
Code:
#!/bin/sh

Quote:
My issue is, the text file does not reset; within the script it keeps the new text replacements.
Actually, the original file itself isn't touched in any way. While sed offers the -i ("in-place") option to overwrite the original file, you don't use it in your script.

Quote:
.
.
.
Only, for reasons described above, it is currently only changing to 'leg'.
.
.
.
Really? I'd expect the output (to stdout) of your script above to be
- the contents of 1.txt with "apple" replaced with "leg"
- the contents of 1.txt
- the contents of 1.txt with "apple" replaced with "arm"


Quote:
Is there any way to reset the contents of the text file back to it's original content
I don't think so - make a working copy and use that.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

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... (7 Replies)
Discussion started by: sohaibs
7 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