sed question for substring search

i have this data where i am looking for a two digit number 01,03,05 or 07.
if not found i should detect that .

this sed command gives me the matching rows . I want the opposite , i want the rows if the match is NOT found .
also the sed command is only looking for 01, can i add 03, 05, 07 to one command ?

-bash-4.4$ sed -n -e 's/^[0-9]\{8\}01/&/p'  a.a
000008050110010201NNN
000008060110010201NNN
000008080110010201NNN
000008090110010201NNN
000008100110010201NNN
000008110110010201NNN
000008120110010201NNN
000008130110010201NNN

something along these lines:

sed -e '/^[0-9]\{8\}0[357]/d' 
1 Like

its giving me all the records ?
e.g in the data below i only want the highlighted record since its 9th and 10th position DO NOT contain either 01,03,05,07

-bash-4.4$ more a.a
000008050110010201NNN
000008060310010201NNN
000008070610010201NNN
000008080110010201NNN
000008090110010201NNN
000008100110010201NNN
000008110110010201NNN
000008120110010201NNN
000008130110010201NNN
-bash-4.4$
-bash-4.4$ sed -e '/^[0-9]\{8\}0[357]/d' a.a
000008050110010201NNN
000008070610010201NNN
000008080110010201NNN
000008090110010201NNN
000008100110010201NNN
000008110110010201NNN
000008120110010201NNN
000008130110010201NNN

sorry forgot 01:

sed -e '/^[0-9]\{8\}0[1357]/d'
1 Like

btw is it possible to just end the sed command even if one nomatching record is found ? i have a large file to search these records and its taking long time to come out .

how about awk:

awk '!/^[0-9]\{8\}0[1357]/{print;exit}' a.a

You can use a so-called rule for this kind of tasks:

/<regexp>/ {
           command1
           command2
           command3
           ....
}

will execute command1 , command2 and command3 only for lines which fit the regexp. Think of it like a "if..then..else"-construct in other languages. You can also invert it by adding an exclamation mark in front of the brackets:

/<regexp>/ ! {
           command1
           command2
           command3
           ....
}

Same as above but only for lines which do NOT fit the regexp.

This way you can use the sed-command q to leave immediately when a certain line is found, Example:

/^--TheEnd--/ {
          q
}

Will quit sed (and hence ignore all following lines) after the first line starting with the string "--TheEnd--" is found.

I hope this helps.

bakunin

1 Like

You said that you want to stop if chars 9 and 10 do not contain 0[1357] , but you include a check for eight leading digits - should that be considered essential as well? If not, try

sed -n '/^.\{8\}0[1357]/! {p;q}' file
000008070610010201NNN