Codecademy-command Line-inputoutput

Source: Internet
Author: User

What happens when do you type this command?

$ echo "Hello"Hello

The echo command accepts the string "Hello" as standardinput, and echoes the string "Hello" to the terminal As standard output.

Let's learn more on standard input, standard output, and standard error:

    1. Standard Input, abbreviated stdin AS, was information inputted into the terminal through the keyboard or input dev Ice.

    2. Standard Output, abbreviated stdout AS, are the information outputted after a process is run.

    3. Standard Error, abbreviated stderr as, was an error message outputted by a failed process.

Redirection reroutes Standard-input, standard-output, and standard error-to-or from a-different location.

How does does redirection work?

$ echo "Hello" > Hello.txt

The > command redirects the standard output to a file. Here, are entered as the standard "Hello" input. The standard output was redirected by to the "Hello" > filehello.txt.

$ cat Hello.txt

The cat command outputs the contents of a file to the terminal. When you type cat hello.txt , the contents of Hello.txt is displayed.

$ cat Oceans.txt > Continents.txt

>Takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output of IS cat oceans.txt redirected tocontinents.txt.

Note that > overwrites all original content incontinents.txt. When you view the output data by typing on continents.txt, you'll see only cat the contents of Oceans.txt.

$ cat Glaciers.txt >> Rivers.txt

>>Takes the standard output of the command in the left and appends (adds) it to the file on the right. You can view the output data of the file with and the cat filename.

Here, the output data of Rivers.txt'll contain

$ Cat < Lakes.txt

<Takes the standard, input from the file, on the right and inputs it into the-the left. Here,lakes.txt is the standard input for the cat command. The standard output appears in the terminal.

The original contents of Rivers.txt with the content ofglaciers.txt appended to it.

$ Cat Volcanoes.txt | Wc

|is a "pipe". |the takes the standard output of the command on the left, and pipes it as standard input to the command on The right. You can think of the "command to command" redirection.

Here cat volcanoes.txt the output of the are the standard input of. In wc turn, the wc command outputs the number of lines, words, an D characters volcanoes.txt in, respectively.

$ Cat Volcanoes.txt | WC | Cat > Islands.txt

Multiple | s can be chained together. Here the standard output of was cat volcanoes.txt "piped" to the wc command. The standard output of was then wc "piped" to cat . Finally, the standard output of was cat redirected to islands.txt .

You can view the output data of this chain by typing cat islands.txt .

$ sort Lakes.txt

sortTakes the standard, input and orders it alphabetically for the standard output. Here, the lakes in is sort lakes.txt listed in alphabetical order.

$ Cat Lakes.txt | Sort > Sorted-lakes.txt

Here, the command takes the standard output from and cat lakes.txt "pipes" it to sort . The standard output of was sort redirected to Sorted-lakes.txt.

You can view the output data by typing on the cat filesorted-lakes.txt.

$ uniq Deserts.txt

uniqStands for "unique" and filters out adjacent, duplicate lines in a file. Here uniq deserts.txt filters out duplicates of "Sahara Desert", because the duplicate of ' Sahara Desert ' directly follows the Previo US instance. The "Kalahari Desert" duplicates is not adjacent, and thus remain.

$ Sort Deserts.txt | Uniq

A more effective-on-the- uniq sort alphabetize a file, and "pipe" the standard output to uniq . Here is piping sort deserts.txt uniq to, all duplicate lines is alphabetized (and thereby made adjacent) and filtered out.

Sort Deserts.txt | Uniq > Uniq-deserts.txt

Here we simply send filtered contents to uniq-deserts.txt and which you can view with the cat command.

$ grep Mount Mountains.txt

grepStands for "Global Regular expression print". It searches files for lines that match a pattern and returns the results. It's also case sensitive. Here, grep searches for ' Mount ' in Mountains.txt.

$ grep-i Mount Mountains.txt

grep -iEnables the command to being case insensitive. Here, grep searches to capital or lowercase strings that match Mount Inmountains.txt.

The above commands is a great-to-get started with grep . If you is familiar with regular expressions, you can use the regular expressions to search for patterns in files.

$ grep-r arctic/home/ccuser/workspace/geography

grep -RSearches all files in a directory and outputs filenames and lines containing matched results. -Rstands for "recursive". Here grep -R searches the/home/ccuser/workspace/geographydirectory for the string "Arctic" and outputs filenames and lines W ITH matched results.

$ GREP-RL arctic/home/ccuser/workspace/geography

grep -RlSearches all files in a directory and outputs only filenames with matched results. -Rstands for ' recursive ' and l stands for ' files with matches '. Here grep -Rl searches the/home/ccuser/workspace/geography directory for the string ' Arctic ' and outputs filenames with match Ed results.

$ sed ' s/snow/rain/' forests.txt

sedstands for "Stream editor". It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to "Find and replace".

Let's look at the expression ‘s/snow/rain/‘ :

    • s: Stands for "substitution". It is always the used when using for sed substitution.
    • snow: The search string, the text to find.
    • rain: The replacement string, the text to add in place.

sedsearches forests.txt for the word "snow" and replaces it with "rain." Importantly, the above command would only replace the first instance of "Snow" on a line.

$ sed ' s/snow/rain/g ' forests.txt

The above command uses the g expression, meaning "global". Here sed searches forests.txt for the word "snow" and replaces it with "rain", globally. All instances of ' snow ' on a line would be turned to "rain".

congratulations! You learned the command line to redirect standard input and standard output. What can we generalize so far?

  • redirection reroutes standard input, standard output, and standard error.

  • The common redirection commands are:

    • >Redirects standard output of a command to a file, overwriting previous content.
    • >>Redirects standard output of a command to a file, appending new content to old content.
    • <Redirects standard input to a command.
    • |Redirects standard output of a command to another command.
  • A number of other commands is powerful when combined with redirection commands:

    • sort: Sorts lines of text alphabetically.
    • uniq: Filters duplicate, adjacent lines of text.
    • grep: Searches for a text pattern and outputs it.
    • sed: Searches for a text pattern, modifies it, and outputs it.
    • Nano is a command line text editor. It works just like a desktop text editor like TextEdit or Notepad, except that it's accessible from the command line and only accepts keyboard input.

        1. The command nano hello.txt opens a new text file named hello.txt in the nano text editor.
        2. "Hello, I am nano"is a text string entered in Nano through the cursor.
        3. The menu of keyboard commands at the bottom for the window allow us to save changes to Hello.txt and Exit Nano. The ^ stands for the Ctrl key.

        4. Ctrl+ O saves a file. ' O ' stands for output.

        5. Ctrl+ X exits the Nano program. ' X ' stands for exit.
        6. Ctrl+ G opens a Help menu.
        7. clearClears the terminal window, moving the command prompt to the top of the screen.

      In this lesson, we'll use the nano to implement

Codecademy-command Line-inputoutput

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.