0

I got some hint that MacOs has different command when using 'sed'.

The original command I have to do was like:

sed -n  '1~36p' /filename

What I got is changing

sed [-an] command [file...]

into

sed [-an] [-e command] [-f command_file] [file...]

so I changed my command as

sed -n -e '1~36p' -f /filename

But it came out with different error like

sed: 1: "1~36p ": invalid command code ~

Did I got wrong command? I want to know how to change properly for Mac.

7
  • 1
    Check your file for unprintable and invisible characters. sed: 1: "1~36p ": - there is a space after p in error message. Commented Dec 1, 2022 at 9:13
  • 1
    ~ is a GNU sed extension and is not available with the default sed that comes with macOS. Install GNU sed with, e.g., Macports or Homebrew if you absolutely need this. Commented Dec 1, 2022 at 9:18
  • first~step form for selecting lines is a GNU extension. You need GNU sed (or use awk). Commented Dec 1, 2022 at 9:19
  • 2
    Also, the argument of -f option is a file that contains sed commands to be run. It is not the input file to be processed. Commented Dec 1, 2022 at 9:33
  • Also, the -e optional unless you're supplying more than one command (e.g. sed -e '1-3 d' -e 's/this/that/' /filename), or supplying arguments in a nonstandard order, or something like that. In other words, the -e and -f are both irrelevant here. The only problem is that ~ is not supported. Commented Dec 1, 2022 at 9:36

1 Answer 1

2

The ~ is a GNU extension to sed. You can use sed -n '1~36p' on macOS, but you'll have to use a different command to get the same effect. The easiest way is to use awk: awk 'NR % 36 == 1' /filename

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.