Korn Shell / Bash,  PowerShell,  Shell Scripting,  Windows Bash

How to Extract Specific Lines from a File: sed, awk, head/tail, grep, and with powershell

In the command line, it’s often necessary to extract only specific lines from a text file – whether it’s a single line or a range of lines. Fortunately, Bash offers several powerful tools for this. In this blog post, I’ll show you the most common methods using sed, awk, head/tail, and grep. Additionally, I will demonstrate an option when using PowerShell.

Extracting Lines with sed

sed (Stream EDitor) is a classic for text manipulation in the shell and is excellent for selectively outputting line ranges.
Output a Single Line (e.g., Line 102)
To output a specific line, such as line 102, you use the p command (print) in combination with the -n option, which suppresses the automatic output of all lines.

Bash
sed -n '102p' your_file.txt

Explanation

-n: Suppresses standard output, so only explicitly requested lines appear.
102p: Prints the 102nd line.

Output a Line Range (e.g., Lines 100 to 110)
For a range, simply specify the start and end lines, separated by a comma, before the p command.

Bash
sed -n '100,110p' your_file.txt

Explanation

100,110p: Prints all lines from line 100 up to and including line 110.

Extracting Lines with awk

awk is a very flexible and powerful text processing tool that can also be used effectively to extract lines based on their number.
Output a Single Line (e.g., Line 102)
awk uses the internal variable NR (Number of Record), which represents the current line number. If the condition NR == 102 is met, the line is printed by default.

Bash
awk 'NR == 102' your_file.txt

Explanation

NR == 102: The line is printed if its number is 102

Output a Line Range (e.g., Lines 100 to 110)
For a line range, you can use a logical AND (&&) to define the desired range.

Bash
awk 'NR >= 100 && NR <= 110' your_file.txt

Explanation

NR >= 100 && NR <= 110: Prints all lines whose line number is between 100 and 110 (inclusive).

Combining head and tail for Line Ranges

Another common method, especially for line ranges, is to combine the head and tail commands. This method is particularly intuitive once you understand the logic.
Output a Line Range (e.g., Lines 100 to 110)
To get lines 100 to 110, we first take the first 110 lines of the file with head. From this result, we then take the last 11 lines with tail. (Why 11? Because 110 – 100 + 1 = 11 lines.)

Bash
head -n 110 your_file.txt | tail -n 11

Explanation
head -n 110 your_file.txt: Outputs the first 110 lines of the file.
|: Pipes the output of head as input to tail.
tail -n 11: Extracts the last 11 lines from the output provided by head.

Line Ranges with nl and grep (with Line Numbers)

Sometimes it’s helpful to output the line numbers directly or to filter by them. Here, nl (number lines) comes into play, which adds line numbers, and grep can then filter these numbered lines.

Bash
nl your_file.txt | grep -E '^(10[0-9]|110)\s'

Explanation
nl your_file.txt: Adds a number to each line. The output will look something like this: 100 Content of line 100.
|: Pipes the numbered output to grep.
grep -E '^(10[0-9]|110)\s': Searches for lines that start with line numbers 100 through 109 or 110, followed by a space (\s).
-E: Enables extended regular expressions.
^: Matches the beginning of the line.
(10[0-9]|110): Searches for numbers from 100 to 109 (10[0-9]) or explicitly for 110.
\s: Matches a whitespace character that separates the line number from the actual content.

Using Powershell to extract lines

On Microsoft Windows systems, PowerShell is the best option. You could use the Get-Content command:

PowerShell
(Get-Content your_file.txt)[9..10]

Explanantion

(Get-Content your_file.txt): Reads your file content, Outputs each line as an array of strings

[9..19]: Prints all lines whose line number is between 9 and 19 (inclusive). Powershell uses array indexing to select lines from the output 9..10 means lines at index 9 through index 10

Important: PowerShell uses 0-based indexing, so:

  • Index 0 = 1st line
  • Index 9 = 10th line
  • Index 10 = 11th line

Tip: You can also use other ranges, like:

  • [0..5] – First 6 lines
  • [10..20] – Lines 11 through 21
  • [-1] – Last line

Which Tool is Best for You?

  • sed: Often the most efficient and direct method for extracting line ranges or single lines, especially with very large files.
  • awk: Very flexible if you need to perform more complex conditions or even calculations based on line numbers or line content.
  • head and tail: A good choice for simple line ranges, once you understand the logic of “cutting” from the top and bottom.
  • nl and grep: Ideal if you want to see the line numbers in the result or if your filtering criteria are more complex and require regular expressions.

Leave a Reply

Your email address will not be published. Required fields are marked *