From owner-freebsd-questions@freebsd.org Sat Mar 6 05:30:41 2021 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id CA34A576470 for ; Sat, 6 Mar 2021 05:30:41 +0000 (UTC) (envelope-from freebsd@dreamchaser.org) Received: from nightmare.dreamchaser.org (ns.dreamchaser.org [66.109.141.57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "dreamchaser.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DstVT40B4z4ngs; Sat, 6 Mar 2021 05:30:41 +0000 (UTC) (envelope-from freebsd@dreamchaser.org) Received: from breakaway.dreamchaser.org (breakaway [192.168.151.122]) by nightmare.dreamchaser.org (8.15.2/8.15.2) with ESMTP id 1265UeZ3091213; Fri, 5 Mar 2021 22:30:40 -0700 (MST) (envelope-from freebsd@dreamchaser.org) Reply-To: freebsd@dreamchaser.org Subject: Re: sed -i empty argument compatibility issue To: Kyle Evans Cc: FreeBSD Mailing List References: <9178f6c5-631a-c2c2-c6b1-8def94a3397b@dreamchaser.org> From: Gary Aitken Message-ID: <42cacf1d-88be-d8a2-9ff4-f82ce378adc7@dreamchaser.org> Date: Fri, 5 Mar 2021 22:25:34 -0700 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (nightmare.dreamchaser.org [192.168.151.101]); Fri, 05 Mar 2021 22:30:40 -0700 (MST) X-Rspamd-Queue-Id: 4DstVT40B4z4ngs X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Mar 2021 05:30:41 -0000 On 3/5/21 10:15 PM, Kyle Evans wrote: > On Fri, Mar 5, 2021 at 11:12 PM Gary Aitken wrote: >> >> On 3/5/21 9:39 PM, Kyle Evans wrote: >>> On Fri, Mar 5, 2021 at 3:03 PM Gary Aitken wrote: >>>> >>>> I'm trying to come up with a fix for a script in a port which invokes sed. >>>> The port comes from a linux environment, and the offending line looks like this: >>>> (This is in a cMake file.) >>>> >>>> COMMAND sed -i "/^# /d" "${outfile}" >>>> >>>> The issue is that linux sed expects the -I or -i extension modifier to >>>> immediately follow the -i. In the above line, the extension is deliberately >>>> missing to provide in-place editing. >>>> >>>> fbsd expects the extension to be separated from the -i by whitespace, or >>>> doesn't work properly when it is empty or immediately follows the -i: >>>> >>>> $ !ls >>>> ls -lt temp.tmp* >>>> -rw------- 1 garya garya 86 Mar 5 13:15 temp.tmp >>>> -rw------- 1 garya garya 86 Mar 5 13:15 temp.tmp_org >>>> $ sed -ifoo "/^# /d" temp.tmp (works on both fbsd & linux)) >>>> $ !ls >>>> ls -lt temp.tmp* >>>> -rw------- 1 garya garya 30 Mar 5 13:48 temp.tmp >>>> -rw------- 1 garya garya 86 Mar 5 13:15 temp.tmp_org >>>> -rw------- 1 garya garya 86 Mar 5 13:15 temp.tmpfoo >>>> $ cp -p temp.tmp_org temp.tmp >>>> $ sed -i"" "/^# /d" temp.tmp (works on linux but not fbsd) >>>> sed: 1: "temp.tmp": undefined label 'emp.tmp' >>>> $ sed -i "" "/^# /d" temp.tmp (works on fbsd but not linux) >>>> $ !ls >>>> ls -lt temp.tmp* >>>> -rw------- 1 garya garya 30 Mar 5 13:49 temp.tmp >>>> -rw------- 1 garya garya 86 Mar 5 13:15 temp.tmp_org >>>> -rw------- 1 garya garya 86 Mar 5 13:15 temp.tmpfoo >>>> >>>> So fbsd works with '-i ""' but linux requires '-i""' >>>> >>>> Does anyone know a work-around for this problem? >>>> >>> >>> My personal favorite trick to bridge the gap here was, as I recall: >>> >>> sed -i'' '' 's/../.../' ${file} >>> >>> IIRC those sed's with an optional backup suffix (Linux, OpenBSD) will >>> accept the immediately following empty string and accept the next >>> empty word as an empty command, while our getopt will effectively >>> ignore the rest of the -i word and use the following optarg as usual. >> >> Unfortunately, times appear to have changed, at least on ubuntu-18.04: >> >> $ sed -i'' '' "/^# /d" temp.tmp >> sed: can't read /^# /d: No such file or directory >> > > Oh, sorry. Try slapping with an -e: > > $ sed -i'' '' -e "/^# /d" temp.tmp nope: $ sed -i'' '' -e "/^# /d" temp.tmp sed: can't read : No such file or directory $ !ls ls -lt temp.tmp* -rw-rw-r-- 1 garya garya 86 Mar 6 05:29 temp.tmp -rw-rw-r-- 1 garya garya 86 Mar 5 20:45 temp.tmp_org Gary