0

I am very new to shell scripting and trying to learn the "sed" command functionality.

I have a file called configurations.txt with some variables defined in it with some string values initialised to each of them. I am trying to replace a string in a file (values.txt) which is present in some other directory by the values of the variables defined. The name of the file is values.txt.

Data present in configurations.txt:-

mem="cpu.memory=4G"
proc="cpu.processor=Intel"

Data present in the values.txt (present in /home/cpu/script):-

cpu.memory=1G
cpu.processor=Dell

I am trying to make a shell script called repl.sh and I dont have alot of code in it for now but here is what I got:-

#!/bin/bash
source /home/configurations.txt
sed <need some help here>

Expected output is after an appropriate regex applied, when I run script sh repl.sh, in my values.txt , It must have the following data present:-

cpu.memory=4G
cpu.processor=Intell

Originally which was 1G and Dell.

Would highly appreciate some quick help. Thanks

3
  • You should probably use m4(1) instead of sed(1). Better yet, learn some higher-level template system such as Template Toolkit or jinja. Commented Apr 27, 2015 at 7:17
  • Sorry mate but I have no idea about it. Can you please help? Commented Apr 27, 2015 at 7:20
  • Is it possible to drop mem= & proc= part? It would be easier using awk, without these parts. Commented Apr 27, 2015 at 7:20

2 Answers 2

1

This question lacks some sort of abstract routine and looks like "help me do something concrete please". Thus it's very unlikely that anyone would provide a full solution for that problem.

What you should do try to split this task into number of small pieces.

1) Iterate over configuration.txt and get values from each line. To do that you need to get X and Y from a value="X=Y" string.

This regex could be helpful here - ([^=]+)=\"([^=]+)=([^=]+)\". It contains 3 matching groups separated by ". For example,

>> sed -r 's/([^=]+)=\"([^=]+)=([^=]+)\"/\1/' configurations.txt
mem
proc
>> sed -r 's/([^=]+)=\"([^=]+)=([^=]+)\"/\2/' configurations.txt
cpu.memory
cpu.processor
>> sed -r 's/([^=]+)=\"([^=]+)=([^=]+)\"/\3/' configurations.txt
4G
Intel

2) For each X and Y find X=Z in values.txt and substitute it with a X=Y.

For example, let's change cpu.memory value in values.txt with 4G:

>> X=cpu.memory; Y=4G; sed -r "s/(${X}=).*/\1${Y}/" values.txt
cpu.memory=4G
cpu.processor=Dell

Use -i flag to do changes in place.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot! This is easy to understand and nice explained. But can you please help me that how can I get the new values saved in values.txt? So that next I open values.txt, I see the new replaced values.
It gives an error saying sed: -e expression #1, char 23: invalid reference \1 on `s' command's RHS when i use sed -i
sed -i -r "s/(${X}=).*/\1${Y}/" values.txt works for me, sed version is sed (GNU sed) 4.2.2
0

Here is an awk based answer:

$ cat config.txt
cpu.memory=4G
cpu.processor=Intel

$ cat values.txt
cpu.memory=1G
cpu.processor=Dell
cpu.speed=4GHz

$ awk -F= 'FNR==NR{a[$1]=$2; next;}; {if($1 in a){$2=a[$1]}}1' OFS== config.txt values.txt 
cpu.memory=4G
cpu.processor=Intel
cpu.speed=4GHz

Explanation: First read config.txt & save in memory. Then read values.txt. If a particular value was defined in config.txt, use the saved value from memory (config.txt).

2 Comments

It does print but doesnt replaces anything in values.txt @anishsane
& is it difficult to send the output to a file & overwrite the old file? command > tmpfile && mv tmpfile values.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.