将 sed 命令从 linux 移植到 mac

porting sed command from linux to mac

我正尝试在我的 mac 上 运行 一个 bash 分析脚本,但 sed 命令无法识别“~”。该脚本在我的 linux 工作站上运行完美。我的脚本看起来像这样;

#!/bin/bash
sed -n '3~9p' file.txt | awk '{print }' > out.dat

当我在 mac 上 运行 时,我得到 "sed: 1: "3~9p": invalid command code ~".

你可以试试这个 awk 而不是 sed,

awk 'BEGIN {s=3;i=9} NR==s{s+=i;print}' file.txt

更直接的方法是,

awk 'BEGIN {s=3;i=9} NR==s{s+=i;print }' file.txt > out.dat

您可以在 mac

上使用 GNU sed
brew install gnu-sed

那么它应该像 linux