Page MenuHomeFreeBSD
Paste P388

String manipulation
ActivePublic

Authored by mr.a_lan.ph on May 18 2020, 2:16 AM.
Objective: Remove instances of the 'nis' string only from these two lines: 'passwd: files nis sss' and 'group: files nis sss' at /path/to/nsswitch.conf (some other 'nis' strings exist at the 'group: files nis sss' line and elsewhere... so we'll leave them as is and narrow down the hits to the '^passwd' and '^shadow' lines only) and call the string-manipulation command/s via sudo.
Any way to keep the whitespaces or tab stops when I use /usr/bin/ex?
[foo@jumpspeeder ~]$ echo 'passwd: files nis sss' >~/nsswitch.conf
[foo@jumpspeeder ~]$ cat ~/nsswitch.conf
passwd: files nis sss
^
|
|
Look here...
[foo@jumpspeeder ~]$ ex -s -c '%s/^\(passwd:\|shadow:\) \+\(files\) \+nis \+\(sss\)/\1 \2 \3/' -c w -c q ~/nsswitch.conf
[foo@jumpspeeder ~]$ cat ~/nsswitch.conf
passwd: files sss
^
|
|
...here too.
Thanks in advance.

Event Timeline

mr.a_lan.ph created this object in space S1 Global.
mr.a_lan.ph added a comment.EditedMay 18 2020, 2:43 AM
As suggested...

[foo@jumpspeeder ~]$ echo  'passwd:     files nis sss' >~/nsswitch.conf
[foo@jumpspeeder ~]$ cat ~/nsswitch.conf
passwd:     files nis sss
         ^
         |
         |
     Look here...

[foo@jumpspeeder ~]$  ex -sc '%s/^\([[:space:]]\)*\(passwd:\|shadow:\)\([[:space:]]\)\+\(files\)\([[:space:]]\)\+nis\([[:space:]]\)\+\(sss\)/\1\2\3\4\5\6\7/' -c w -c q ~/nsswitch.conf
[foo@jumpspeeder ~]$  cat ~/nsswitch.conf
passwd: files  sss
       ^
       | 
       |
  ...here too.


Still no joy. 


Thanks in advance.
Works like a charm...


[foo@jumpspeeder ~]$ echo  'passwd:     files nis sss' >~/nsswitch.conf
[foo@jumpspeeder ~]$ cat -v  ~/nsswitch.conf
passwd:     files nis sss
         ^
         |
         |
     Look here...
[foo@jumpspeeder ~]$ ex -sc 'g/^\([[:space:]]\)*\(passwd:\|shadow:\)/s/nis[[:space:]]//' -c w -c q ~/nsswitch.conf
[foo@jumpspeeder ~]$ cat -v ~/nsswitch.conf
passwd:     files sss
       ^
       | 
       |
  ...here too.


This'll do.


Thanks a lot for the sugestion/s.
mr.a_lan.ph added a comment.EditedMay 18 2020, 5:27 AM
I've a problem with 'sh -c' which doesn't seem to honor the '\(' character...

As a test (played with ~/nsswitch.conf before I edit the actual /etc/nsswitch.conf), here it throws out...


[foo@jumpspeeder ~]$ sudo -E -k sh -c 'a=~/nsswitch.conf; cp -fpv "${a}" "${a}".$(date +"%Y%m%d%H%M%S").baka; ex -sc 'g/^\([[:space:]]\)*\(passwd:\|shadow:\)/s/nis[[:space:]]//' -c w -c q "${a}"'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `a=~/nsswitch.conf; cp -fpv "${a}" "${a}".$(date +"%Y%m%d%H%M%S").baka; ex -sc g/^([[:space:]])*(passwd:|shadow:)/s/nis[[:space:]]// -c w -c q "${a}"'
[foo@jumpspeeder ~]$



I'm fairly certain that the problem is in the 'ex -sc 'g/^\(' part... I just don't know the root cause. 

Any tips on how to resolve this?  Thanks in advance.
mr.a_lan.ph edited the content of this paste. (Show Details)May 18 2020, 5:57 AM
mr.a_lan.ph added a comment.EditedMay 20 2020, 2:12 PM
mr.a@razorcrest:~ % uname -ns
FreeBSD razorcrest
mr.a@razorcrest:~ % echo ${SHELL}
/bin/csh
mr.a@razorcrest:~ % sudo -k echo 'passwd:     files nis sss'>/tmp/nsswitch.conf  
mr.a@razorcrest:~ % sudo -k echo 'shadow:     files nis sss'>>/tmp/nsswitch.conf  
mr.a@razorcrest:~ % sudo -k cat /tmp/nsswitch.conf  
passwd:     files nis sss
shadow:     files nis sss
mr.a@razorcrest:~ % sudo -k -E sh -c 'export a=/tmp/nsswitch.conf; cp -fpv "${a}" "${a}".$(date +"%Y%m%d%H%M%S").baka;' sudo -k ex -sc 'g/^\([[:space:]]\)*\(passwd:\|shadow:\)/s/nis[[:space:]]//' -c w -c q /tmp/nsswitch.conf
‘/tmp/nsswitch.conf’ -> ‘/tmp/nsswitch.conf.20200520103634.baka’

That doesn't seem to work...

[mr.a@jumpspeeder ~]$ sudo -k cat /tmp/nsswitch.conf
passwd:     files nis sss
shadow:     files nis sss


But when I elevate to Charlie Root...

[mr.a@jumpspeeder ~]$ sudo su -


..then do this (that the same set of commands, yeah), 

[root@jumpspeeder ~]# ex -sc 'g/^\([[:space:]]\)*\(passwd:\|shadow:\)/s/nis[[:space:]]//' -c w -c q /tmp/nsswitch.conf
[root@jumpspeeder ~]# cat /tmp/nsswitch.conf
passwd:     files sss
shadow:     files sss


...target string 'nis' is gone.

This makes me scratch my head... tsk, tsk. I really want to stick to sudo all throughout the session, so please help me find what's wrong with my logic.

Thanks in advance,

mr.a