Issue
In the sed
below I am trying to remove all lines that have ID_
and end in an odd number. Both of the commands execute but return the file unchanged.
file
ID_0111 xxx
ID_0112 xxx
ID_0113 xxx
ID_0114 xxx
xxxxxxxxxxxxxxxx
desired
ID_0112 xxx
ID_0114 xxx
xxxxxxxxxxxxxxxx
sed
sed '/ID_[13579]/d' file.txt
sed 's|ID_[13579]$| |g'
Solution
You may use this sed
:
sed '/^ID_[^ ]*[13579] /d' file
ID_0112 xxx
ID_0114 xxx
xxxxxxxxxxxxxxxx
Here regex pattern /^ID_[^ ]*[13579] /
matches a line that starts with ID_
followed by 0 or more non-space characters and ends with [13579]
before matching a space.
Or else, using awk
you can do this:
awk '$1 !~ /^ID_/ || $1 ~ /[02468]$/' file
ID_0112 xxx
ID_0114 xxx
xxxxxxxxxxxxxxxx
Answered By - anubhava Answer Checked By - Mary Flores (WPSolving Volunteer)