How to remove only the first occurrence of a line in a file using sed

MOHAMED

I have the following file

titi
tata
toto
tata

If I execute

sed -i "/tat/d" file.txt

It will remove all the lines containing tat. The command returns:

titi
toto

but I want to remove only the first line that occurs in the file containing tat:

titi
toto
tata

How can I do that?

devnull

You could make use of two-address form:

sed '0,/tat/{/tat/d;}' inputfile

This would delete the first occurrence of the pattern.

Quoting from info sed:

 A line number of `0' can be used in an address specification like
 `0,/REGEXP/' so that `sed' will try to match REGEXP in the first
 input line too.  In other words, `0,/REGEXP/' is similar to
 `1,/REGEXP/', except that if ADDR2 matches the very first line of
 input the `0,/REGEXP/' form will consider it to end the range,
 whereas the `1,/REGEXP/' form will match the beginning of its
 range and hence make the range span up to the _second_ occurrence
 of the regular expression.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to insert file contents on match using sed - first occurrence only

From Dev

Using sed how to remove last character only in the first line

From Dev

how to remove both first and last line of csv file using sed

From Dev

How to use sed to replace only the first occurrence?

From Java

How can I remove the first line of a text file using bash/sed script?

From Dev

How can I remove the first line of a file with sed?

From Dev

remove only the first blank line sed

From Dev

How to match only first occurrence of space at line

From Dev

How to print only the first non-blank line using sed

From Dev

Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

From Dev

Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

From Dev

Want to replace a string only at first occurrence using sed

From Dev

How can I get sed to only match to the first occurrence of a character?

From Dev

Printing and deleting the first line of a file using `sed`

From Dev

Remove line from php file using sed

From Dev

How to replace first n no. of occurrence of a string using sed and/or awk?

From Dev

sed command to remove first occurrence of a word

From Dev

SED/awk remove string until first occurrence

From Dev

How to add a line after nth occurrence of a keyword using sed?

From Dev

How to *return* *only* the nth occurrence of a regex match using sed?

From Dev

How to replace every nth occurrence of pattern using sed ONLY

From Dev

Sed : replace words only with first occurrence of string in the line not till last match

From Dev

How to replace the first line using sed?

From Dev

Using AWK or Sed how can I remove trailing carriage return and a line feeds before first txt

From Dev

How to remove first occurrence of a letter?

From Dev

How can I change the third occurrence of a word in a file using sed?

From Dev

Matching only the first occurrence in a line with Regex

From Dev

Delete till first occurrence of colon using sed

From Dev

Append text to first occurrence of a pattern using sed

Related Related

  1. 1

    how to insert file contents on match using sed - first occurrence only

  2. 2

    Using sed how to remove last character only in the first line

  3. 3

    how to remove both first and last line of csv file using sed

  4. 4

    How to use sed to replace only the first occurrence?

  5. 5

    How can I remove the first line of a text file using bash/sed script?

  6. 6

    How can I remove the first line of a file with sed?

  7. 7

    remove only the first blank line sed

  8. 8

    How to match only first occurrence of space at line

  9. 9

    How to print only the first non-blank line using sed

  10. 10

    Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

  11. 11

    Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

  12. 12

    Want to replace a string only at first occurrence using sed

  13. 13

    How can I get sed to only match to the first occurrence of a character?

  14. 14

    Printing and deleting the first line of a file using `sed`

  15. 15

    Remove line from php file using sed

  16. 16

    How to replace first n no. of occurrence of a string using sed and/or awk?

  17. 17

    sed command to remove first occurrence of a word

  18. 18

    SED/awk remove string until first occurrence

  19. 19

    How to add a line after nth occurrence of a keyword using sed?

  20. 20

    How to *return* *only* the nth occurrence of a regex match using sed?

  21. 21

    How to replace every nth occurrence of pattern using sed ONLY

  22. 22

    Sed : replace words only with first occurrence of string in the line not till last match

  23. 23

    How to replace the first line using sed?

  24. 24

    Using AWK or Sed how can I remove trailing carriage return and a line feeds before first txt

  25. 25

    How to remove first occurrence of a letter?

  26. 26

    How can I change the third occurrence of a word in a file using sed?

  27. 27

    Matching only the first occurrence in a line with Regex

  28. 28

    Delete till first occurrence of colon using sed

  29. 29

    Append text to first occurrence of a pattern using sed

HotTag

Archive