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)
Code:
hostname="fbsd"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="NO"

# Wifi config
wlans_iwn0="wlan0"
ifconfig_wlan0="WPA SYNCDHCP"

# For X.org
moused_enable="YES"
dbus_enable="YES"
hald_enable="YES"
slim_enable="YES"

# Disable sendmail
sendmail_enable="NO"
sendmail_msp_queue_enable="NO"
sendmail_outbound_enable="NO"
sendmail_submit_enable="NO"
and the ex(1) command line I am using to replace wlan string with XXX
Code:
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
Code:
SunOS SF2545 5.10 Generic_150400-16 sun4u sparc SUNW,Sun-Fire-V890
, and ex(1) version is
Code:
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.
Code:
% ex
/tmp/vi.DIzeUJdcCe: new file: line 1
:ve
Version 2.1.2 (2012-11-02) The CSRG, University of California, Berkeley.
:q!
% cat bad.txt
interface=" interface wlan"
% cat good.txt
interface="wlan"
% ex -sc '%s/interface/iface/g|x' good.txt
% cat good.txt                      
iface="wlan"
% 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:
Code:
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.
 
Back
Top