[SOLVED] Replacing text with content from another file in an order
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Replacing text with content from another file in an order
I am not that good and scripting and got stuck with a requirement while writing a script, I have been trying to replace contents of same text in a file that comes multiple times with the contents of another file which comes in each line. Below is an example file1
I want to replace "FAIL" from the file1 with entries from file 2 so that first "FAIL" gets replaced with first entry from file 2 which is "SUCCESS" and this continues. The end result should be like following,
I tried using a for loop in below format, but it doesn't work. How do increment to replace first occurrence with the first entry from file2 and second occurrence with second entry from file2, and so on?
Could you please help?
for a in `cat file2`; do sed 's/FAIL/$a/1' file1
Last edited by Sayan Acharjee; 06-14-2016 at 11:28 PM.
What I used in the script:
- a while loop to read the file
- an array to hold the replacements (if you need to read these from a file, then do so)
- sed to do the actual replacing
- a counter
- echo to print the information to STDOUT
Being a Red Hat Certified Engineer, you will figure this out easily.
I have 3 shell scripts, one is pure bash, second uses sed and third uses awk ... all seems fairly straight forward.
Your original solution is quite close ... may I suggest looking at the curly braces and /q options on the following page: http://www.grymoire.com/Unix/Sed.html
As grail pointed out, better to tell the solution even if it wasn't needed later as it was marked as solved.
Code:
declare -a arr;
#to store the texts to be replaced in the file in the array
arr=($(cat file1 |grep "(*)"|grep -v Comp|awk -F"(" {'print $4'}|awk -F")" {'print $1'}));
count=0;
while read -r i
do
sed -i "s/${arr[$count]}/$i/g" file1
(( count ++ ))
done < file2
Last edited by Sayan Acharjee; 06-17-2016 at 05:40 AM.
Interesting Your initial example gave the impression that we knew the word FAIL and hence only had to replace it, but your solution points to not knowing what might be in the required spot ... which
is better as I initially was wondering how you would ever change FAIL for SUCCESS
Here is a bit of a tidy up for you:
Code:
arr=($(awk -F"[()]+" '/REGEX/{getline;print $4}' file1))
count=0
while read -r new
do
sed -i "s/${arr[count++]}/$new/g" file1
done<file2
I would point out that I have tidied the code but it will not produce the results you were wanting .. this will update the original file (I presumed that "test" was a typo as there is no file with that name)
so that all items are changed to the first entry from second file and then no other changes get made as all the original names have now been changed.
So from the example, all FAIL words were changed to SUCCESS, but when loop performs further iterations the word FAIL is nowhere to be found so no future changes are made
Maybe in the future you would like to present examples that actually reflect what you have and what you need then as it appears nothing about the original question was actually correct.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.