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:
class="lang-bash prettyprint-override">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 charsle
- ale
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 containle
.
Answered By - Wiktor Stribiżew Answer Checked By - Mary Flores (WPSolving Volunteer)