1

I castrated the following sed syntax in order to remove from file any host that not contain linux/LINUX

 sed -i '/linux\|LINUX/!d' file

example

linux01.moon.com
solaris.moon.com
UNIX.foot.com

it will give

linux01.moon.com

but now we have other scenario that domains also include linux name as

linux01.room_linux.com
linux02.gt.com
linux03.klist.com
sol.linux.com

it will give

linux01.room_linux.com
linux02.gt.com
linux03.klist.com
sol.linux.com

instead of

    linux01.room_linux.com
    linux02.gt.com
    linux03.klist.com

so how to change my sed syntax in order to ignore the domain name if include linux or LINUX ?

  • Are you sure you don't want to select hosts with linux in the name as soon as they also have linux in the domain? Sure? – Philippos Aug 13 at 11:50
  • I want to ignore what is in the domain , so if domain include any linux/LINUX then it will not remove it – yael Aug 13 at 11:52
  • But then why don't you want linux01.room_linux.com? It has linux in the host name. – Philippos Aug 13 at 11:52
  • sorry I agree - you right – yael Aug 13 at 11:59
  • see my update , again I am very sorry about this - you right – yael Aug 13 at 12:01
1

You need to fix the pattern to the start of the line with ^ and then allow only other characters than the dot ([^.]*) until your keyword is found:

sed -E '/^[^.]*(linux|LINUX)/!d'

Note that you need to add braces around your alternative patterns. I switched to extended regular expressions to make it more readable.

  • the problem is that - its removed all hosts in the file , so we get file empty – yael Aug 13 at 12:07
  • we have redhat 7.2 OS , on which OS you try it? – yael Aug 13 at 12:07
  • This can't be an OS version issue. If your sed accepts option -E it will work. Or do your lines contain other stuff before the host? – Philippos Aug 13 at 12:18
  • I means I use the syntax - sed -iE '/^[^.]*(linux|LINUX)/!d' ( why -i flag cause remove of all hosts ? ) – yael Aug 13 at 12:18
  • Never concatenate options after -i! This will make the E an argument (backup file extension) for the -i option and lose the -E option! Use -i -E or -Ei. – Philippos Aug 13 at 12:20
0
$ sed -e '
   h
   s/\..*//;y/linux/LINUX/;/LINUX/!d
   g
' inp.file

This is the Posix sed approach you could take.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.