-1

I need to parse a message (git commit message) and retrieve some information from there (ID's for main chapters). This messages not respect a pattern on main chapters position (Eg. Features is not always before defects).

Note: Any main chapter ("Features:","Some_random_text:" or other "string:") can exist or not in the message. If exists , retrieve ID and label (Eg. "Features:" )

Expected output: "Features: 4554773, 4554773", if exists

In the rows below you can find a message example:

Some text here

Features:
  - ID:  4554773
  - ID: 23423234

Some_random_text:
  - another stuff here
  - ID: 2255444

=========
Another info here

Where I fail is extracting ID's for "Features:" and ID'S for "Some_random_text:".

Programming language used: Python3.X

My existing code:

def get_features(commit_message):
    try:
        my_result=commit_message.split("Features:")[1]
    except IndexError:
        return("-")
    result=my_result.split("ID:")[1].split()[0].split("\n")[0]
    return (result)

Any help will be appreciated.

Georgian
  • 53
  • 1
  • 8
  • Do you need all the values of ID together, or do they need to be further split into IDs for "Features:" and "Some_random_text:"? – Andrew Morton Dec 09 '20 at 14:09
  • I need splitted for Features and "Some_random_text:" . To gel bulk ID's is not a problem . The only pattern I saw till now is "some_text: any_thing another_text:" – Georgian Dec 09 '20 at 14:10
  • So it looks like you need another `split` in there. Also, the question should say exactly how it fails: does it get no values, the wrong values, something else...? – Andrew Morton Dec 09 '20 at 14:12
  • We could have exception: - no ID's, no chapter or no text in message – Georgian Dec 09 '20 at 14:15
  • You do not have to use `regex`. This seems like a valid `yaml` file. Use `yaml.safe_load` to load the file in as a dictionary and you can then access the `feature` object – Onyambu Dec 10 '20 at 17:29

1 Answers1

0

an awk script:

awk '/^Features/,/^$/ { if($2=="ID:") a[$3]=$3 }
     /^Some_random_text/,/^$/{ if ($2=="ID:") b[$3]=$3}
     END{ printf("Features: "); 
          for(i in a){ printf "%s,",i }; 
          printf("\nrandomtxt: "); 
          for(i in b){ printf "%s,",i}}'  inputfile

with output:

Features: 4554773,23423234,
randomtxt: 2255444,

Is this awk script returning expected output?

Luuk
  • 4,913
  • 3
  • 17
  • 21