I have a text file, and I'm trying to extract the last block of text that is separated by two newline characters. For example, if I have a command like this:
```bash
echo -e 'prennblocknfirstnnpostnnbklocknLASTnnsomechars'
```
I want to be able to isolate and get just this portion:
```
block
LAST
```
Any ideas on how to achieve this?
1 Answer
Another option is to utilize `sed`. This command can help extract what you're looking for:
```bash
MATCH=$(sed -nr "s/nn(.*?)nn[^n]*\$//p" <<< "$VARIABLE") && echo "$MATCH"
```
Make sure to escape properly; this will handle the last block well. Just remember, it might fail if `somechars` includes newlines, so adjust accordingly!
It’s true, the regex could be unreliable with certain inputs. You might want to refine it further to avoid issues with unexpected newline characters. Consider adopting a more specific approach with negative assertions if your data varies!