I would like to extract variables from a text file with Bash, Perl and Regex.
The file looks like this (and is already read into the variable $str):
Filename: XXXXX Type: XXX Size: XXXX Unimportant thing: XXXX Filename: YYYYY Type: YYY Size: YYYY Unimportant thing: YYYY
I need Filename, Type and Size for each block. An array would be the best but a strings containing these variables separated by a given character is also acceptable.
However, sometimes some of the fields (e.g. Size or Type) is missing. I would like to omit these records, so I think I need a Regex which can match through multiple lines.
I tried the following:
perl -pe 's/Filename: ([^\n]*)\nType: ([^\n]*)\nSize: ([^\n]*)\n/\1\t\2\t\3\n/' <<< $str
but this printed out the original text without modification.
Then I tried it without the p commandline parameter (I hoped this way the whole file will be processed instead of iterating lines):
perl -e 's/Filename: ([^\n]*)\nType: ([^\n]*)\nSize: ([^\n]*)\n/\1\t\2\t\3\n/' <<< $str
This one did not print anything (empty result).
Then I tried adding print in front of the Regex because I thought maybe the removal of -p caused that Perl does not know that I want the results printed:
perl -e 'print s/Filename: ([^\n]*)\nType: ([^\n]*)\nSize: ([^\n]*)\n/\1\t\2\t\3\n/' <<< $str
Still no success (empty result).
What am I missing?
I would like this as a one line perl command.