Home

Working with Commands

Objectives

I learned how to:

Task 2: Using the tee command

In this task, I used the tee command to display the output to both the screen and a file.

The tee command reads the standard input. In this example, the standard input is hostname. The tee command outputs the hostname to the screen (in the shell) and the designated file, which is file1.txt.

Steps I took:

  1. To validate that I was in the /home/ec2-user folder, I entered pwd and pressed Enter.
  2. From my current location in the terminal, I entered hostname | tee file1.txt and pressed Enter.

The output from the command hostname | tee file1.txt was ip-10-0-10-81.us-west-2.compute.internal.

From the following output, I could see the standard input for tee in the output of the command hostname. The tee command wrote the hostname to the file1.txt and to the screen.

[ec2-user@ ~]$ hostname | tee file1.txt
ip-(xx-xx-xx-xx).(region).compute.internal

To confirm that the file1.txt file had been created, I entered ls and pressed Enter.

In the current directory, there are two items present: companyA and file1.txt

[ec2-user@ ~]$ ls
companyA file1.txt

Task 3: Using the sort command and pipe operator

In this task, I used the sort command to reorder the list within the test.csv file. I also used the pipe operator to search for the factory in Paris.

Steps I took:

  1. To validate that I was in the /home/ec2-user folder, I entered pwd and pressed Enter.
  2. I entered cat > test.csv and pressed Enter.
  3. I entered the following list as shown and pressed Enter.
    [ec2-user@ ~]$ cat > test.csv
    Factory, 1, Paris
    Store, 2, Dubai
    Factory, 3, Brasilia
    Store, 4, Algiers
    Factory, 5, Tokyo
  4. When I was done, I pressed CTRL+D to exit the file.
  5. To verify that the test.csv file had been created, I entered ls and pressed Enter.
  6. Now that I had created a few items within the test.csv file, I used the sort command to reorder the list. I entered sort test.csv and pressed Enter.

The output looked like the following image. Because I used the sort command with no options, it sorted the list with the default action by alphabetical order, which is why Factory is listed before Stores. The command then sorts by numerical order.

When the command sort test.csv was ran, it sorted the contents within the file in the following order: Factory 1 Paris, Factory 3 Brasilia, Factory 5 Tokyo, Store 2 Dubai, and Store 4 Algiers.

[ec2-user@ ~]$ sort test.csv
Factory, 1, Paris
Factory, 3, Brasilia
Factory, 5, Tokyo
Store, 2, Dubai
Store, 4, Algiers

To look for the factory named Paris using the pipe (|) operator, I entered find | grep Paris test.csv and pressed Enter.

In the following output, find | grep Paris test.csv searches and lists the content of the test.csv file and redirects the results to the grep command where it searches for the Paris pattern.

When the command grep Paris test.csv is ran, it searches for the word Paris and returns the following: Factory, 1, Paris.

[ec2-user@ ~]$ find | grep Paris test.csv
Factory, 1, Paris

Task 4: Using the cut command

In this task, I used the cut command to edit the test.csv file.

Steps I took:

  1. To validate that I was in the /home/ec2-user folder, I entered pwd and pressed Enter.
  2. I entered cat > cities.csv and pressed Enter.
  3. I entered the following list as shown and pressed Enter.
    Dallas, Texas
    Seattle, Washington
    Los Angeles, California
    Atlanta, Georgia
    New York, New York
  4. When I was done, I pressed CTRL+D to exit the file.
  5. Next, I used the cut command to cut sections from lines of text by character. I used the -d (delimiter) option, the , option, and the -f (field) option. The combined command and options extract the first field of each record.
  6. I entered the following command cut -d ',' -f 1 cities.csv

As I could see from the following output, the cut command removed everything after the ,.

After the cut command is ran, the following is left: Dallas, Seattle, Los Angeles, Atlanta, and New York.

[ec2-user@ ~]$ cut -d ',' -f 1 cities.csv
Dallas
Seattle
Los Angeles
Atlanta
New York

Additional Challenge

I was tasked to use only the sed command to make changes or do all the changes in one line. (I could use command chaining using the pipe character (|).)

I remembered that the sed command is mainly used to replace some text in a file for different text.

sed 's/word being replaced/replacement word/' file name

The sed command searches the file text for an occurrence of the first string, and will replace any matches with the second.

I used the sed command to replace the first comma (,) with periods (.) in both the cities.csv and test.csv files.

Output:

Dallas. Texas
Seattle. Washington
Los Angeles. California
Atlanta. Georgia
New York. New York
Factory. 1, Paris
Store. 2, Dubai
Factory. 3, Brasilia
Store. 4, Algiers
Factory. 5, Tokyo

Related Topics