From owner-freebsd-questions@freebsd.org Sat Mar 6 04:39:51 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 A8C42574644 for ; Sat, 6 Mar 2021 04:39:51 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DssMq4J37z4kjX for ; Sat, 6 Mar 2021 04:39:51 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) (Authenticated sender: kevans) by smtp.freebsd.org (Postfix) with ESMTPSA id 7CD8AF2C7 for ; Sat, 6 Mar 2021 04:39:51 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: by mail-qt1-f170.google.com with SMTP id t13so2140795qta.11 for ; Fri, 05 Mar 2021 20:39:51 -0800 (PST) X-Gm-Message-State: AOAM533+5GGpmsQs5j0ldfbb5mY/u5UNlr+ySaD2hUAM1fjWc399n77q aH9zVl5m4iVseEeKWGQLFd6ft6Zpk1WdYZXbR3s= X-Google-Smtp-Source: ABdhPJwb1gs5wHNZRIiBclvwvR3fErQAcQB8eA5yFIEpT/6vEC7ieamHfaNSNJL8u1B/ZxsjyvEl8agFsNegGE0d33k= X-Received: by 2002:ac8:5bd1:: with SMTP id b17mr12111848qtb.53.1615005590810; Fri, 05 Mar 2021 20:39:50 -0800 (PST) MIME-Version: 1.0 References: <9178f6c5-631a-c2c2-c6b1-8def94a3397b@dreamchaser.org> In-Reply-To: <9178f6c5-631a-c2c2-c6b1-8def94a3397b@dreamchaser.org> From: Kyle Evans Date: Fri, 5 Mar 2021 22:39:39 -0600 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: sed -i empty argument compatibility issue To: freebsd@dreamchaser.org Cc: FreeBSD Mailing List Content-Type: text/plain; charset="UTF-8" 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 04:39:51 -0000 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.