sd
sd copied to clipboard
Delete line based on match
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
?
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
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 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