I am learning sed nowadays. There is this tilde '~' that confuses me more than anything.

For instance, the command

sed -n '1~2 w output.txt' input.txt

is printing the odd numbered lines (to file output.txt).

In the same tutorial, it is also given that it skips lines from 1 up to 2 and then it starts to print the lines next to line number 2 in case we use

sed -n '1~2 p' input.txt

What is the absolute interpretation of this tilde?

  • What, exactly, does the tutorial say for the second example? "skips lines from 1 up to 2" is not an accurate description of what that command does. – JigglyNaga Aug 2 '16 at 8:42

This is a GNU sed extension:


Match every step'th line starting with line first. For example, sed -n 1~2p will print all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line, starting with the second. first can be zero; in this case, sed operates as if it were equal to step. (This is an extension.)

Your two examples matches the same lines, but the first writes (w) them to a file while the second prints (p) them to wherever standard output is going.

In general, always refer to the manual of the command. The manual is seldom ambiguous. The application of a command by combining several aspects of its functionality may be difficult to explain in a tutorial sometimes (and sometimes a tutorial tries to make things easier to understand by rephrasing the same statement in many ways, which could lead to confusion), but the manual is the definite reference for each bit of what the aggregated command does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.