codecademy-command line-inputoutput

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

时间: 2024-11-13 08:21:46

codecademy-command line-inputoutput的相关文章

Can&#39;t use Subversion command line client: svn

使用Intellij IDEA的svn时提示出错:Can't use Subversion command line client: svn. 当我在使用svn,Checkout一个项目后,然后将其导入到Intellij idea中,出现这样的报错!经过google后,发现了问题,我的问题是:我安装的TortoiseSVN工具,本身是带有command-line功能的(我没有安装)如图: 所以报这个错误.如果安装的TortoiseSVN工具,本身是不带有command-line功能的,必须要安装

解决MySQL5.6 Warning: Using a password on the command line interface can be insecure

MySQL5.6在使用名文的密码登陆时,会出现:Warning: Using a password on the command line interface can be insecure 当然这样对于平常的登陆会无所谓,如果在脚本里使用使用的话,就会有问题: 解决这种问题的方法是需要在my.cnf中配置即可: 在my.cnf中加入如下配置 [mysqladump] user=my_name password=my_pass 重启MySQL 即可 以后再使用mysqldump命令就不需要加上任

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 to install a number of programs for building your Android project, this article is for you. You will need only JDK, the Android SDK platform tools and

Command line option syntax error.type Command /? for help

电脑装思维导图的时候,报错显示"Command line option syntax error.Type Command /? for help."就查了一下,原来是系统没有C++2005,需要安装,就上网下载了一个vcredist_x86.exe,但是双击安装,仍然出现这个错误. 没办法,接着上网查吧,是什么原因呢?网上说是因为该文件安装不支持中文安装路径,然后我就把文件夹改成了英文名称的,但是双击还是出现这个错误.可能有的人用这种方法成功了吧~本着没有解决不了的问题的思想,接着奋

How To use RHEVM Command Line?

本文简单的描述下如何连接rhev shell以及简单的使用.关于更详细的用法请参考官方文档. 1.如何连接到rhevm? 要想连接到rhevm,必须拥有一个有效的证书.此证书一般安装完rhevm后会自动产生.下面是如何获取到证书. [[email protected] ~]# wget -O rhevm.cer http://rhevm.xzxj.edu.cn/ca.crt --2014-05-02 09:29:34-- http://rhevm.xzxj.edu.cn/ca.crt Resol

【转载】Data Science at the Command Line

Data Science at the Command Line Data Science at the Command Line is a new book written by Jeroen Janssens. This website contains information about the upcoming workshop in London, the webcast from August 20th, instructions on how to install the Data

Mac OS X Command Line

关于 man 命令 虽然有上千条命令,每条命令还有许多可选参数和具体的使用方式,但是你却不需要记住这些命令.你只需要记住一个:man 大多数命令都会包含一个使用指南,会告诉你任何你需要知道的关于这个命令的所有细节,在命令行中输入 man command-name 即可获取.例如,你想知道ls这个命令怎么使用,输入man ls即可进入使用指南页面. 使用指南往往很长,所以你可以使用▲(上箭头)或▼(下箭头)来上下移动,使用 来翻页,输入/和关键字来按照关键字搜索,按Q来退出使用指南页面. 那么——

MySQL5.6 Using a password on the command line interface can be insecure

最近把MySQL从5.5升到5.6以后,mysqldump居然不好用了,提示:  代码如下 复制代码 [[email protected] ~]# /usr/local/mysql/bin/mysqldump  -uroot -proot db > bak.sqlWarning: Using a password on the command line interface can be insecure. 翻译过来是:在命令行界面上使用密码可以是不安全的. 这让人有点郁闷,5.5用的一直都很爽,

Xcode Command Line Tools(命令行工具)

OS X 10.9 Mavericks正式发布,免费更新,立即去更新看看效果. 不过升级后安装命令行工具(Command Line Tools)时发现官网没有clt的下载安装包了,原来改了,使用命令在线安装. 打开终端,输入命令:xcode-select --install                                                                                                           

Ubuntu wireless network connection in command line

After installed the the graphical card driver in Ubuntu, the gnome desktop was not working anymore, needs to install and setup the wireless network in command line, herewith the procedure, 1, scan the available wifi network, sudo iwlist scan 2, recei