Filter by contents of neighbouring rows

Hi, folks

Is there an easy way to filter based on the contents of values in adjacent rows? (a bit like the -A “after” and -B “before” options in ‘grep’)

For example, if I only wanted rows containing String1 in column 2 when the next row contained String2 in column 2 … and I wanted the option of keeping all pairs of rows (or ranges of rows) that matched, would that be possible?

1 Timestamp1 String1
2 Timestamp2 String2

6 Timestamp6 String1
7 Timestamp7 String3

18 Timestamp18 String1
19 Timestamp19 String2

Would result in:

1 Timestamp1 String1
2 Timestamp2 String2
18 Timestamp18 String1
19 Timestamp19 String2

It’s not critical if the timestamp value in column 1 in the second row is lost, so if it meant provisionally concatenating column 2 of the second row onto the first’s column 2, and then filtering, that wouldn’t be a problem - I just couldn’t work out a way to do that!

Apologies if I’m attempting to use EDT for something it’s not designed for; if needs be, I could always pre-process the file in something else first. I’m still trialing EDT and am just testing out some potential uses for it.

Thanks,
Steve

You need to copy the string column and work with “offset” to move the values in the copied column up or down. Then you can compare the values of the original String column and the copied one with “if” function. The result of “if” move with an offset again, afterwards you can filter and remove the interim columns. Maybe there are more elegant ways, but it works. See attached example.

neighbouring rows.transform (5.2 KB)

2 Likes

As @Olaf says you can do it by using Offset.

1 Like

Hi,

I am assuming that the strings that you like to match have numbers at their end and if that is the case, here is the solution.

I just took your sample text and added some more rows.

Transform file:
neighbouringrows.transform (5.4 KB)

2 Likes

Belated thanks to @Olaf, @Admin, and Anonymous for all of your swift and helpful replies (sorry - I was away for a few days without access to my computer, so couldn’t try out your suggestions).

This gave me just the help I needed to write exactly the set of transforms I wanted … and the confidence to buy a licence (which I’ve now done :slight_smile: ).

Best wishes,
Steve

1 Like