我知道如果sed
是GNU版本,可以像
$ sed --version
但这在OS X中不起作用。我该怎么做?
答案 0 :(得分:16)
这可能不是您正在寻找的答案,但您无法做到。 Mac OS X sed
无法显示版本号。
二进制文件中甚至没有版本号:
$ strings $(which sed)
$FreeBSD: src/usr.bin/sed/compile.c,v 1.28 2005/08/04 10:05:11 dds Exp $
$FreeBSD: src/usr.bin/sed/main.c,v 1.36 2005/05/10 13:40:50 glebius Exp $
$FreeBSD: src/usr.bin/sed/misc.c,v 1.10 2004/08/09 15:29:41 dds Exp $
$FreeBSD: src/usr.bin/sed/process.c,v 1.39 2005/04/09 14:31:41 stefanf Exp $
@(#)PROGRAM:sed PROJECT:text_cmds-88
malloc
%lu: %s: unexpected EOF (pending }'s)
0123456789/\$
%lu: %s: command expected
%lu: %s: invalid command code %c
%lu: %s: command %c expects up to %d address(es), found %d
%lu: %s: unexpected }
%lu: %s: extra characters at the end of %c command
%lu: %s: command %c expects \ followed by text
%lu: %s: extra characters after \ at the end of %c command
%lu: %s: filename expected
w command
read command
branch
label
%lu: %s: empty label
%lu: %s: substitute pattern can not be delimited by newline or backslash
%lu: %s: unterminated substitute pattern
%lu: %s: extra text at the end of a transform command
%lu: %s: unterminated regular expression
%lu: %s: expected context address
realloc
%lu: %s: whitespace after %s
%lu: %s: duplicate label '%s'
%lu: %s: RE error: %s
%lu: %s: \ can not be used as a string delimiter
%lu: %s: newline can not be used as a string delimiter
%lu: %s: unbalanced brackets ([])
bin/sed
Unix2003
123456789
%lu: %s: \%c not defined in the RE
%lu: %s: unescaped newline inside substitute pattern
%lu: %s: unterminated substitute in regular expression
%lu: %s: more than one number or 'g' in substitute flags
%lu: %s: overflow in the 'N' substitute flag
%lu: %s: no wfile specified
%lu: %s: bad flag in substitute command: '%c'
%lu: %s: transform pattern can not be delimited by newline or backslash
%lu: %s: unterminated transform source string
%lu: %s: unterminated transform target string
%lu: %s: transform strings are not the same length
%lu: %s: undefined label '%s'
%lu: %s: unused label '%s'
Eae:f:i:ln
setlinebuf() failed
stdout
"%s"
..."
-i may not be used with stdin
stdin
rename()
%s: %s %s
in-place editing only
works for regular files
%s: name too long
%s/.!%ld!%s
%s: %s
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
first RE may not be empty
RE error: %s
%lu: %s: \%d not defined in the RE
COLUMNS
\abfrtv
\%03o
答案 1 :(得分:5)
查看text_cmds
中sed
的各种版本,但没有太多变化。
10.0.0
从1998年秋季开始使用NetBSD sed
10.3.0
从2002年秋季开始使用FreeBSD sed
,但手册保持不变,
添加-E
扩展正则表达式和-i
就地编辑。10.4.0
对就地编辑进行了一些修复,并将手册页更改为
从2004年秋季开始的FreeBSD手册页。10.5.0
从2005年秋季开始更新为FreeBSD sed,添加了-l
行缓冲输出和就地更多优化/修复
编辑。10.6.0
在手册页中添加了对compat(5)
的引用。可以使用sed
或sw_vers -productversion
来检查操作系统的版本,而不是uname -r
实用程序:
case $(sw_vers -productversion) in
10.[012].*) echo no in-place editig, EREs, or line-buffered output;;
10.[45].*) echo no line-buffered output;;
10.[6789].*) echo newest version as of writing;;
10.1[01].*) echo newest version as of writing;;
esac
(BSD实用程序未进行版本控制,因为它们被视为其操作系统的一部分)
答案 2 :(得分:1)
由于(如 kdhp 所说)BSD 将 sed 等工具视为操作系统的一部分,因此您可以将操作系统版本视为工具版本,因此:
sed_version="$(sw_vers -productVersion)"
echo $sed_version
#output> 10.15.7
man
日期sed_version="$(sed --version 2>&1 | grep -q GNU && sed --version | sed '1!d;s/.* //' || man sed | sed '$!d; s/ *BSD *//g')"
echo $sed_version
#output> May 10, 2005
-i
等功能由于 BSD 版本没有 --version
选项,您可以触发它。 (这比执行 [[ $(uname) == Darwin ]]
更安全,因为一些被迫使用 Mac 的 Linux 人会使 sed
调用 gsed
并给自己带来比他们想象的更多的麻烦。
function is_gnu_sed(){
sed --version >/dev/null 2>&1
}
function sed_i_wrapper(){
if is_gnu_sed; then
$(which sed) "$@"
else
a=()
for b in "$@"; do
[[ $b == '-i' ]] && a=("${a[@]}" "$b" "") || a=("${a[@]}" "$b")
done
$(which sed) "${a[@]}"
fi
}
date > /tmp/date
sed_i_wrapper -i s/2/X/g /tmp/date
cat /tmp/date
#output> Tue Dec X9 19:00:13 UTC X0X0