Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete a line from a file with sed following a pattern

I have a file with the following lines:

auth_service_oauth_secret_accfrsh
auth_service_oauth_secret_accfrshv12
auth_service_oauth_secret_accfrshv13

I would like to be able to remove only the first line, where my pattern will be only "accfrsh", but I tried with multiple sed commands and it’s not working. All lines with the pattern "accfrsh" are removed or nothing change.

I tried the below sed commands (it doesn’t matter with -e or -i) :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sed -e "/accfrsh/d" my_file (remove all lines)
sed -e 's/\<accfrsh\>//g' my_file (nothing changes)
sed -e "/'^'accfrsh'$'/d" my_file (nothing changes)

>Solution :

This might work for you (GNU sed):

sed '/_accfrsh\b/d' file

Delete lines which contain _accfrsh followed by a word boundary.

Compare it to:

sed '/_accfrsh\B/d' file

N.B. The \b is related to /< and /> which denote the start and end of a word boundary (see here).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading