sd icon indicating copy to clipboard operation
sd copied to clipboard

Delete line based on match

Open nacardin opened this issue 4 years ago • 3 comments

With sed, it is posible to do this:

sed -i'' '/myhostname/d' /etc/hosts

This will delete any line in /etc/hosts which contains "myhostname".

Is there a way to achieve this using sd?

nacardin avatar Sep 05 '20 19:09 nacardin

I am trying to do something similar as well.

I have a test file with ignores and I would like to strip out this statement

void test_longest_known_isogram(void) { TEST_IGNORE(); TEST_ASSERT_TRUE(is_isogram("efe")); }

and I thought I would be able to strip it with this, sd ' TEST_IGNORE();' '' test/isogram.c

but I couldn't get it to work so now I'm not sure if its supported by sd

matthiasdebernardini avatar Sep 29 '20 13:09 matthiasdebernardini

Try sd '(\t| )*TEST_IGNORE\(\);\r?\n' '' test/isogram.c. ( and ) have to be escaped so that they are not interpreted as regex syntax. To delete whole lines the line endings \r\n or \n must be included in the pattern. The part at the beginning of the regex (\t| )* makes sure that any indentation is also taken into account.

SimplyDanny avatar Mar 13 '21 14:03 SimplyDanny

@SimplyDanny is right where the line ending has to be explicitly included to match the full line instead of $. The most succinct way I know of is \r?\n

The current equivalent would be

$ cat issue.txt
a
myhostname
b
a myhostname b
c

$ sd --preview '^.*myhostname.*\r?\n' '' issue.txt
a
b
c

CosmicHorrorDev avatar Oct 22 '23 00:10 CosmicHorrorDev