What happens when you type this command?

$ echo "Hello"
Hello

The echo command accepts the string "Hello" asstandard input, and echoes the string "Hello" back to the terminal as standard output.

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

  1. standard input, abbreviated as stdin, is information inputted into the terminal through the keyboard or input device.

  2. standard output, abbreviated as stdout, is the information outputted after a process is run.

  3. standard error, abbreviated as stderr, is 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 redirection work?

$ echo "Hello" > hello.txt

The > command redirects the standard output to a file. Here, "Hello" is entered as the standard input. The standard output "Hello" is redirected by > to the 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 are 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 cat oceans.txt is redirected tocontinents.txt.

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

$ cat glaciers.txt >> rivers.txt

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

Here, the the output data of rivers.txt will contain

$ cat < lakes.txt

< takes the standard input from the file on the right and inputs it into the program on 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 this as "command to command" redirection.

Here the output of cat volcanoes.txt is the standard input of wc. in turn, the wc command outputs the number of lines, words, and characters involcanoes.txt, respectively.

$ cat volcanoes.txt | wc | cat > islands.txt

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

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

$ sort lakes.txt

sort takes the standard input and orders it alphabetically for the standard output. Here, the lakes insort lakes.txt are listed in alphabetical order.

$ cat lakes.txt | sort > sorted-lakes.txt

Here, the command takes the standard output fromcat lakes.txt and "pipes" it to sort. The standard output of sort is redirected to sorted-lakes.txt.

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

$ uniq deserts.txt

uniq stands 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 previous instance. The "Kalahari Desert" duplicates are not adjacent, and thus remain.

$ sort deserts.txt | uniq

A more effective way to call uniq is to call sort to alphabetize a file, and "pipe" the standard output to uniq. Here by pipingsort deserts.txt to uniq, all duplicate lines are 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, which you can view with the cat command.

$ grep Mount mountains.txt

grep stands for "global regular expression print". It searches files for lines that match a pattern and returns the results. It is also case sensitive. Here, grep searches for "Mount" in mountains.txt.

$ grep -i Mount mountains.txt

grep -i enables the command to be case insensitive. Here, grepsearches for capital or lowercase strings that match Mount inmountains.txt.

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

$ grep -R Arctic /home/ccuser/workspace/geography

grep -R searches all files in a directory and outputs filenames and lines containing matched results. -R stands for "recursive". Heregrep -R searches the /home/ccuser/workspace/geographydirectory for the string "Arctic" and outputs filenames and lines with matched results.

$ grep -Rl Arctic /home/ccuser/workspace/geography

grep -Rl searches all files in a directory and outputs only filenames with matched results. -R stands for "recursive" and lstands for "files with matches". Here grep -Rl searches the/home/ccuser/workspace/geography directory for the string "Arctic" and outputs filenames with matched results.

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

sed stands 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 used when using sed for substitution.
  • snow: the search string, the text to find.
  • rain: the replacement string, the text to add in place.

In this case, sed searches forests.txt for the word "snow" and replaces it with "rain." Importantly, the above command will 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 will be turned to "rain".

Congratulations! You learned how to use 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 are 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 is 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 of 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. clear clears the terminal window, moving the command prompt to the top of the screen.

      In this lesson, we'll use nano to implement

codecademy-command line-inputoutput的更多相关文章

  1. Linux Command Line learning

    https://www.codecademy.com/en/courses/learn-the-command-line Background The command line is a text i ...

  2. How to build .apk file from command line(转)

    How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want ...

  3. Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.最近使用SVN工具时,Checkout出项目到本地后后,然后将其导入到Intellij idea中开发,在提交svn代码的时候,出现这样的错误:Can't use Subversion comma ...

  4. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  5. Chrome-Console( Command Line API Reference)

    来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Comma ...

  6. logoff remote desktop sessions via command line tools

    This trick I learned from my one of ex-college.  In Windows servers, only two remote desktop session ...

  7. 使用intellij的svn时提示出错: Can't use Subversion command line client: svn.Errors found while svn working copies detection.

    使用Intellij的svn时提示出错:Can't use Subversion command line client: svn. Errors found while svn working co ...

  8. ubuntu16.04安装virtualbox5.1失败 gcc:error:unrecognized command line option ‘-fstack-protector-strong’

    系统:ubuntu16.04.1 软件:Virtualbox-5.1 编译器:GCC 4.7.4 在如上环境下安装Vbx5.1提示我在终端执行/sbin/vboxconfig命令 照做 出现如下err ...

  9. Linux Command Line 笔记(1)

    Yunduan CUI graphical user interfaces make easy tasks easy, while command line interfaces make diffi ...

  10. Can't use Subversion command line client:svn

    在Intellij IDEA里checkout东西时出先这个错误提示:Can't use Subversion command line client:svn Subversion command l ...

随机推荐

  1. Yii2 数据操作Query Builder(转)

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  2. NancyFx中使用自带的IOC容器

    /// <summary> /// Cors扩展 /// </summary> public static class IPipelinesExtensions { /// & ...

  3. java读写excel文件

    近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情. 读取的文件主要分两类:xls文件.xlsx文件.xls文件的相关操作用的 ...

  4. height:100%和height:auto的区别

    一直不明白height:100%和height:auto的区别,最近在制作前端页面时都用了height:100%;overflow:hidden; ,可是有些浏览器出现莫名的奇妙的问题,但换成heig ...

  5. sql事务和锁

    摘自:http://www.cnblogs.com/lxconan/archive/2011/10/20/sql_transaction_n_locks_1.html 最近在项目中进行压力测试遇到了数 ...

  6. IIS服务器运行一段时间后卡死,且无法打开网站(IIS管理无响应,必须重启电脑)

    问题描述: 公司希望使用IIS配合网站显示一些订单跟进的情况并展示出来,所以我们在一台演示的Win7 Pro电脑上安装了IIS,但使用了一段时间后发现每过几天页面就无法正常访问了,而且打开IIS管理器 ...

  7. Windows如何使用jstack跟踪异常代码

    维护服务器时,会出现java进程在CPU.内存.硬盘上总是出现异常情况. 如何找到是哪些代码出现这些异常呢? 本文使用jstack来实现这个需求 工具/原料   java jstack Process ...

  8. SQL Server中TEXT类型字段值在数据库中追加字符串方法

    在数据上我们往往会遇到ntext大文本类型,这种类型如果和 nvarchar类型相加会出现问题,所以有一中方法可以解决这种问题. 使用的sql   函数: TEXTPTR:返回要更新的 text.nt ...

  9. JavaScript的面临的9个陷阱

    虽然不是什么很高深的技术问题,但注意一下,会使您的编程轻松些,即所谓make life easier. 笔者对某些陷阱会混杂一些评点. 1.   最后一个逗号 如这段代码,注意最后一个逗号,按语言学角 ...

  10. GNU KHATA——开源的会计管理软件

    导读 GNU Khata是一个会计工具. 或者,我应该说成是一系列的会计工具集合,它就像经济管理方面的Evernote一样.它的应用是如此之广,以至于它不但可以用于个人的财务管理,也可以用于大型公司的 ...