Question

There is a text file like that

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false

daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/srv/ftp:/bin/false
http:x:33:33:http:/srv/http:/bin/false

How to replace in VI all characters in strings 2-4 from 2nd to 5th to 'X'?

UPD: it's smth like: :2,4s//X/g I guess I need a regular expression

UPD2: :2,4s/^\(.\)...\|^$/\1XXX/ | 2,4s/^$/ XXX/

Was it helpful?

Solution

Vim only

Try this command:

:2,4s/\%2c.../XXX/

Where:

  • 2,4 is for in strings 2-4
  • \%2c... is for from 2nd to 5th
  • XXX is for to 'X'

Both Vim and Vi

As Vi doesn't have \%c, this command should be used instead:

:2,4s/^\(.\).../\1XXX/

Resources

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top