Delete all lines that contain a substring between two indexes

Issue

I need to delete all the lines containing some string in a given file if it appears between the 3rd and 10th position (character).

This is my sed command:

sed '/^.\{3\}le.\{10\}/d' file-name

Solution

You can use

sed '/^.\{3\}.\{0,5\}le/d' filename

The POSIX BRE regular expression means:

  • ^ – start of string
  • .\{3\} – any three chars
  • .\{0,5\} – any zero to five chars
  • le – a le substring.

So, 3 + 5 is 8, and le length is 2, so the pattern only removes lines that contain le in between the third and tenth positions.

You may also use awk:

awk '!index(substr($0,4,6), "le")' filename

Here,

  • substr($0,4,6) – gets a 6-char substring starting with the fourth char
  • !index(..., "le") – returns true if the substring above does not contain le.

Answered By – Wiktor Stribi┼╝ew

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published