Search code examples
bashshellsedrc

Parsing resource Visual Studio .rc file with the sed command


How can i use sed for pase .rc file? And I need get from resource file blocks STRINGTABLE

...

STRINGTABLE
BEGIN
    IDS_ID101    "String1"
    IDS_ID102    "String2"
END

...

and result need output to str_file.h

std::map<int, std::string> m = {
    {IDS_ID101, "String1"},
    {IDS_ID102, "String2"},
};

How do I write a command in sed so that I can get this result?

I write this command, but this not help me

sed '/^BEGIN$/{N;N;s/BEGIN\(.*\)END/replaced \1/}/g' ${RC_FILE} > test_rc.h

Solution

  • awk seems more fit for this task than sed:

    awk '
        /^STRINGTABLE/ {
            in_block = 1
            print "std::map<int, std::string> m = {"
            next
        }
        in_block && /^END/ {
            in_block = 0
            print "};"
            next
        }
        in_block && match($0,/".*"/) {
            print "    {" $1 ", " substr($0,RSTART,RLENGTH) "},"
        }
    ' "$RC_FILE" > test_rc.h