2

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
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
AlexZel
  • 83
  • 1
  • 5

2 Answers2

1

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
Fravadona
  • 13,917
  • 1
  • 23
  • 35
1

This might work for you (GNU sed):

sed -En '/STRINGTABLE/{
         :a;N;/\nEND/!ba
         s/.*BEGIN(\s+)(\S+)\s+(\S+)(\s+)(\S+)\s+(\S+).*/std::map<int, std::string> m = {\1{\2, \3},\4{\5, \6},\n};/p
         }' file

Gather up lines between STRINGTABLE and END and then using pattern matching and back references, format the output as required.

potong
  • 55,640
  • 6
  • 51
  • 83