Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
# Duplicates have been removed leaving only the unique names.
[intro_to_linux]$ cat fruits.txt | grep berry | sort | uniq
Bilberry
Blackberry
Blueberry
Boysenberry
Cloudberry
Cranberry
Elderberry
Goji berry
Gooseberry
Honeyberry
Huckleberry
Juniper berry
Marionberry
Mulberry
Raspberry
Salal berry
Salmonberry
Strawberry

[intro_to_linux]$ cat fruits.txt | grep berry | sort | uniq | wc –l
18

...

Code Block
[intro_to_linux]$ cat fruits.txt | grep berry | sort | uniq > berries.txt
[intro_to_linux]$ cat berries.txt
Bilberry
Blackberry
Blueberry
Boysenberry
Cloudberry
Cranberry
Elderberry
Goji berry
Gooseberry
Honeyberry
Huckleberry
Juniper berry
Marionberry
Mulberry
Raspberry
Salal berry
Salmonberry
Strawberry

[intro_to_linux]$ cat berries.txt | wc -l
18
Info

Although a simple example, it demonstrates the principle of piping four commands into a single call.

Without using the pipe command you would have had to create intermediate files, to store results, after each command.

As you get more confident, you can create more elaborate pipelines of comandscommands.

...

Example: Pipe from ls command

...

Code Block
[intro_to_linux]$ ls -R
[intro_to_linux]$ ls -R | grep "Feb"
February
./data/2022/February:
Feb
./data/2023/Feb:

# Ignore case.
[intro_to_linux]$ ls -R | grep -i "Feb"
feb
./data/2021/feb:
february_01_2021.tx
February
./data/2022/February:
Feb
./data/2023/Feb:

...

Exercises

Info

Questions:

  1. How does the wc command work? What are its options?

  2. How does the sort command work? What are its options?

  3. How does the uniq command work? What are its options?

  4. How many unique varieties of beans are there in the vegetables.txt file?

...

Answers

Info

4

...

. How many unique varieties of beans are there in the vegetables.txt file?

  • How do you deal with “soy beans” vs “Soy Beans”

...

  • ?

Code Block
[intro_to_linux]$ cat vegatables.txt | grep -i beans | sort | uniq -i | wc -l
12

...