How does sed -n work?

0195

The sed -n option suppresses the default output of sed, which means that it will not print every line of the input file to the standard output. Instead, it allows you to control what gets printed by using specific commands.

Here's how it works:

  1. Suppress Output: By using -n, sed will not automatically print each line after processing it.

  2. Explicit Printing: You can use the p command to explicitly tell sed which lines to print. For example:

    sed -n '1,5p' filename.txt

    This command will print only lines 1 to 5 of filename.txt.

  3. Combining with Other Commands: You can combine -n with other commands to filter and print specific lines based on patterns. For example:

    sed -n '/pattern/p' filename.txt

    This will print only the lines that match "pattern".

Using -n is useful when you want to avoid cluttering the output with lines that you do not need to see.

0 Comments

no data
Be the first to share your comment!