Remove lines that match a string

Have a shortcut that loops through a bunch of tax and I want to check if each line contains a string and if it does remove it. Basically, think of it as a pipe to grep -v.

I’ve trying to replace .string.\n with nothing, or with “” but this doesn’t work. If I leave off the \n I can strip the line, but it leaves a blank line or a line containing “”.

Are you using regex with multi line turned on?

Regex yes, otherwise the replacement would work at all I don’t think, but I don’t see a multi line setting.

Because there isn’t one and I’m dumb for answering without testing/looking myself. My bad. I’ll stop being lazy and spin up a similar example and test on my end.

Are you sure you only have one line break and not a line break and a carriage return? \n\r

Nope, not sure, it’s text built in a repeat item that gets names and birthdates from my contacts. If I add \n or \r or \n\r to the end of the regex it dos not match

If I add $ to the end, it martches but leaves a blank line. If I leave off the $ it matches and leaves a blank line.

Does this simple example work for you?

Crud. That does work. This doesn’t

What exactly are you passing in? A quick replication worked for me I think.

Also, you may wish to consider what happens for the last line. If the last line contains 1604, it won’t match as there is no new line to match against.

I was thinking the same thing. An optional would do the trick, no?


It’s basically the birthday script from another thread on this discourse. So it is passed birthday, name, birthdate.

I’d figured that from the “1604” :wink:, but that’s ‘generally ‘ what you might be passing in. It isn’t ‘exactly ‘ what you are passing in.

It works for that format too.

… and yes, making the new line optional is better…

But, the earlier screenshot shows a repeat loop, not a piece of text being passed in. The referenced birthday shortcut example from another thread (that I created) had loops, but it would seem that this sort of exclusion would be applied to the final result. Not to each contact … as the inferred text from a contact would only coerce the name and not the birthday.

Perhaps you could capture your input on one of the loop iterations, place a sanitised version in a text action, run the replace against that, and demonstrate the effect you are getting?

Does this have to be done in Shortcuts? If not I have written a Drafts action that can be easily modified:

That is a good point. I’d thought it’d be better to strip them out inside the loop, but obviously that’s not necessary and I’m not sure why I thought it was.

Moving the match outside of the repeat works fine.