How can I extract the last block of text separated by double newlines?

0
0
Asked By CuriousCat123 On

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

Answered By SedMaster008 On

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!

RegexNinja21 -

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.