Shell ex to replace text

Hi there,

I am trying to use ex(1) to replace a pattern in a text file. The behavior is not as I expected, below is the text file (copy of rc.conf)
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable

# Wifi config
ifconfig_wlan0="WPA SYNCDHCP"

# For

# Disable sendmail
and the ex(1) command line I am using to replace wlan string with XXX
ex -sc '%s/wlan/XXX/g|x' rc.conf.copy
ex(1) simply hangs and waits for a user input. Indeed, I would expect it to write changes and exit.

FreeBSD version is 10.2-RELEASE, shell is zsh(1) and ex(1) version is Version 2.1.2 (2012-11-02) The CSRG, University of California, Berkeley.

Any ideas?
Another ex(1) implementation on SunOS 5.10/Solaris 10 behaves as expected.

Test system is
SunOS SF2545 5.10 Generic_150400-16 sun4u sparc SUNW,Sun-Fire-V890
, and ex(1) version is
Version SVR4.0, Solaris 2.5.0

For my script, I will use sed(1), which requires a temp file to be used.


As SirDice says, sed(1) has an in-place option. Tip: when giving -i an empty value, make sure there is a space before it: -i ''. Note the space before the single quotes.
Apparently, there is a bug in the ex(1) implementation - nvi/nex - used in FreeBSD; if the phrase that would be substituted by a new one appears more than once in a line, the ex(1) command fails.

Although, having a newer version of the software on it (2.3.1 as opposed to 2.1.2 on 10.2-RELEASE), the ex(1) clone in 11.0-CURRENT suffers from the same bug as well.

I spent some time to debug the problem today (recompiled ex(1) with debug option on a 11.0-CURRENT VM and run it in gdb(1), traced the code to some extend) but I think there is no harm in submitting a bug report in the mean time :)

I filed a bug report (bug 202740) and will see how it goes.... It is also worth noting that, the nvi package in Debian (which , I believe, is also from the very same source used in FreeBSD) has the same issue. The Debian package is an older port, though.

Below is the demonstration of the issue on 10.2-RELEASE, the last command always fails.
% ex
/tmp/vi.DIzeUJdcCe: new file: line 1
Version 2.1.2 (2012-11-02) The CSRG, University of California, Berkeley.
% cat bad.txt
interface=" interface wlan"
% cat good.txt
% ex -sc '%s/interface/iface/g|x' good.txt
% cat good.txt                      
% ex -sc '%s/interface/iface/g|x' bad.txt
gpatrick , thanks for the code! But I am after a solution that would require no dependencies, so gawk and ksh are out of my scope for now.
Last edited:
Maybe, but why use it when sed(1) can do that more easily and in a more standard way?
Well, depends what the standard is :). On HP-UX -i is not an option. Definitely good portable way is the one Juha Nurmela mentioned.
I use the:
echo "matchline/s/what/where\nw\nq" | ed -s /path/to/file
way, but basically it's the same. Priceless if you need that on various UNIX (and Linux) OS types.