1

I am trying to code a bash script in a build process where we only have a few tools (like grep, sed, awk) and I am trying to replace a value in an ini file with a value from a regular expression match in another.

So, I am matching something like "^export ADDRESS=VALUE" in file export_vars.h and putting VALUE into an ini file called config.ini in a line with "ADDRESS=[REPLACE]". So, I am trying to replace [REPLACE] with VALUE with one command in bash.

I have come across that sed can take an entire file and insert it into another with a command like

sed -i -e "/[REPLACE]/r export_vars.h" config.ini

I need to somehow refine this command to only read the pattern match from export_vars.h. Does anyone know how to do this?

2
  • Just to clarify, you wish for the line in export_vars.h (e.g. export ADDRESS=VALUE) to replace a line already in config.ini (e.g. ADDRESS=VALUE)? I assume the thing you are trying to match here is ADDRESS and you wish to replace VALUE in this example? Commented Apr 20, 2017 at 13:25
  • Yes - or just replace the [REPLACE] in the ini file with VALUE. I guess either would work. Commented Apr 20, 2017 at 13:27

2 Answers 2

1

sed is for simple substitutions on individual lines, that is all. You need to be looking at awk for what you're trying to do. Something like:

awk '
BEGIN { FS=OFS="=" }
NR==FNR { 
    if ( $1 == "export ADDRESS" ) {
        value = $2
    }
    next
}
{ sub(/\[REPLACE\]/,value); print }
' export_vars.h config.ini

Untested, of course, since you didn't provide testable sample input/output.

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

8 Comments

Thanks, that worked perfectly. Need to learn awk more, apparently, because we have other tasks like this.
You're welcome. To learn awk, read the book Effective Awk Programming, 4th Edition, by Arnold Robbins. All other books are obsolete, and all online tutorials are buggy and/or misleading. If you are considering doing anything in sed that uses constructs other than s, g, and p (with -n) then you are using the wrong tool. Sed is for s/old/new/, that is all, and even then has caveats (e.g. it doesn't understand strings). All of the other arcane sed constructs became obsolete in the mid-1970s when awk was invented and are useful today only for mental exercise, never for production software.
I've never heard of it but from the title 2 things spring to mind: 1) You do NOT need any book to tell you how to use sed properly (s/old/new/ - done!), 2) it has the word Hacks in it - who in their right mind would want to learn how to write hacks instead of idiomatically correct, elegant code? Oh I see, it was written by the guy with the terrible awk "tutorial" at thegeekstuff - no, avoid that like the plague.
@EdMorton, right, and I didn't find it at any online book-shop ... He wrote I have been adding 1’s and 0 ‘s for more than 17 years in the IT industry ...
I wonder what answer he gets when he adds 1 plus 0 :-). Take a look at his tutorial 3. Awk RS Example: Record Separator variable. He says Awk RS defines a line - no it doesn't, it separates records. He shows RS="\n\n" to read blank-line separated records - no you don't you use RS="" and RS="\n\n" would be gawk-specific anyway due to multi-char RS. I've provided corrections to the examples on his site in the past but he doesn't listen.
|
1

Another in awk:

$ awk '/ADDRESS/{if(a!="")$0=a;else a=$NF}NR>FNR' export_vars.h config.ini
ADDRESS=VALUE

Explained:

$ awk '
/ADDRESS/ {                 # when ADDRESS is found in record
    if(a!="") $0=a          # if a is set (from first file), use it
    else a=$NF }            # otherwise set a with the last field
NR>FNR                      # print all record of the last file
' export_vars.h config.ini  # mind the order

This solution does not tolerate space around = since $0 is replaced with $NF from the other file.

Comments

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.