# LeetCode 195: Tenth Line

## Problem Restatement

We are given a text file named `file.txt`.

We need to print only the tenth line of the file.

If the file has fewer than ten lines, the script should print nothing.

Example input:

```text
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
```

Expected output:

```text
Line 10
```

## Input and Output

| Item | Meaning |
|---|---|
| Input | A file named `file.txt` |
| Output | The tenth line only |
| Missing tenth line | Print nothing |
| Language | Bash shell script |

## Examples

Input:

```text
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
```

Output:

```text
Line 10
```

If the file contains only three lines:

```text
Line 1
Line 2
Line 3
```

The output is empty, because there is no tenth line.

## First Thought

We only care about one line.

So we do not need to process or transform the whole file.

We need a command that can identify line number `10` and print exactly that line.

Several Unix tools can do this:

| Tool | Idea |
|---|---|
| `awk` | Print the line where `NR == 10` |
| `sed` | Print only line 10 |
| `head` and `tail` | Keep the first 10 lines, then take the last one |

## Key Insight

In `awk`, `NR` means the current record number.

For normal text files, each record is one line.

So the tenth line is exactly the line where:

```awk
NR == 10
```

When this condition is true, `awk` prints the current line.

## Algorithm

Read the file line by line.

For each line:

1. Track its line number.
2. If the line number is `10`, print it.
3. Ignore all other lines.

With `awk`, this becomes one command.

## Correctness

`awk` processes the file one line at a time.

For each line, `NR` stores the current line number.

The condition `NR == 10` is true only for the tenth line.

Therefore, `awk 'NR == 10' file.txt` prints the tenth line and no other line.

If the file has fewer than ten lines, the condition is never true, so nothing is printed.

## Complexity

Let `n` be the number of lines in the file.

| Metric | Value | Why |
|---|---|---|
| Time | `O(n)` | In the simple form, `awk` may scan through the file |
| Space | `O(1)` | It processes one line at a time |

## Implementation

```bash
# Read from the file file.txt and output the tenth line to stdout.
awk 'NR == 10' file.txt
```

## Code Explanation

The command is:

```bash
awk 'NR == 10' file.txt
```

`awk` reads `file.txt` line by line.

`NR` is the current line number.

When `NR` equals `10`, the condition is true.

In `awk`, when a condition is true and no explicit action is given, the default action is to print the current line.

So this command prints only line ten.

## Alternative Implementations

Using `sed`:

```bash
sed -n '10p' file.txt
```

The `-n` option disables automatic printing.

The command `10p` prints only line `10`.

Using `head` and `tail`:

```bash
head -n 10 file.txt | tail -n 1
```

`head -n 10` keeps the first ten lines.

`tail -n 1` prints the last line from those ten lines.

If the file has at least ten lines, that line is the tenth line.

## Testing

Create a sample file:

```bash
cat > file.txt << 'EOF'
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
EOF
```

Run the solution:

```bash
awk 'NR == 10' file.txt
```

Expected output:

```text
Line 10
```

Test a file with fewer than ten lines:

```bash
cat > file.txt << 'EOF'
Line 1
Line 2
Line 3
EOF
```

Expected output is empty.

Test a file with more than ten lines:

```bash
cat > file.txt << 'EOF'
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
EOF
```

Expected output:

```text
Line 10
```

