Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete line based on match #99

Open
nacardin opened this issue Sep 5, 2020 · 3 comments
Open

Delete line based on match #99

nacardin opened this issue Sep 5, 2020 · 3 comments
Labels
C-question Category: Further information is requested

Comments

@nacardin
Copy link

nacardin commented Sep 5, 2020

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?

@matthiasdebernardini
Copy link

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

@SimplyDanny
Copy link
Contributor

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.

@CosmicHorrorDev CosmicHorrorDev added the M-needs triage Meta: Maintainer label me! label May 17, 2023
@CosmicHorrorDev
Copy link
Collaborator

@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 CosmicHorrorDev added C-question Category: Further information is requested and removed M-needs triage Meta: Maintainer label me! labels Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-question Category: Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants