2019独角兽企业重金招聘Python工程师标准>>>

We've seen how to redirect from a command to a file. We can also redirect the other way, from a file to a command. This involves redirecting from thestandard output of the file to the standard inputof the command.

In our last screen, the file beer.txt ends up looking like this:

99 bottles of beer on the wall...

Take one down, pass it around, 98 bottles of

beer on the wall...

The Linux sort command will sort the lines of a file in alphabetical order. If we pass the -r flag, the lines will be sorted in reverse order.

sort < beer.txt

The above code will sort each of the lines inbeer.txt in order.

Instructions

  • Use the sort command to sort the lines of beer.txt in reverse order

/home/dq$ sort -r < beer.txt

Take one down,pass it around,98 bottles of beer on the wall...

00 bottles of beer on the wall....

###################################################################

3: The Grep Command

Sometimes, we'll want to search through the contents of a set of files to find a specific line of text. We can use the grep command for this.

grep "pass" beer.txt

The above command will print any lines inbeer.txt where the string pass appears, and highlight the string pass.

We can specify multiple files by passing in more arguments:

grep "beer" beer.txt coffee.txt

This will show all lines from either file that contain the string beer.

Instructions

  • Make a file called coffee.txt that has two lines of text in it:

    Coffee is almost as good as beer,
    But I could never drink 99 bottles of it
    
  • Use the grep command to search beer.txt and coffee.txtfor the string bottles of

~$ echo 'Coffee is almost as good as beer,\nBut I could never drink 99 bottles of it' > coffee.txt
~$ grep "bottles of" beer.txt coffee.txt

##########################################################################

 4: Special Characters

Like we did in the last screen, sometimes we'll want to execute commands on a set of files. There were only 2 files in the last screen though,beer.txt and coffee.txt. But what if we wanted to search through all 1000 files in a folder? We definitely wouldn't want to type out all of the names. Let's say we have the following files in a directory:

beer.txt

beer1.txt

beer2.txt

coffee.txt

better_coffee.txt

If we wanted to search for a string in beer1.txtand beer2.txt, we could use this command:

grep "beer" beer1.txt beer2.txt

We could also use a wildcard character, ?. ? is used to represent a single, unknown character. We could perform the same search we did above like this:

grep "beer" beer?.txt

The wildcard above will match both beer1.txtand beer2.txt. We can use as many wildcards as we want in a filename.

Instructions

  • Create empty files called beer1.txt and beer2.txt.
  • Use grep and the ? wildcard character to search for beer in both beer1.txt and beer2.txt

~$ touch beer1.txt
~$ touch beer2.txt
~$ grep "beer" beer?.txt

####################################################

5: The Star Wildcard

We learned about the ? wildcard character in the last screen, but there are also other wildcard characters. Let's say we again have the following files in a directory:

beer.txt

beer1.txt

beer2.txt

coffee.txt

better_coffee.txt

We can use the * character to match any number of characters, including 0.

grep "beer" beer*.txt

The above command will search for the stringbeer in beer.txt, beer1.txt, andbeer2.txt. We can also use the wildcard to match more than 1 character:

grep "beer" *.txt

The above command will search for the stringbeer in any file that has a name ending in.txt.

We can use wildcards anytime we would otherwise enter a filename. For example:

ls *.txt

The above command will list any files with names ending in .txt in the current directory.

Instructions

  • Use grep and the * wildcard character to search for beer in all the files ending in .txt in the home directory

~$ grep "beer" *.txt

转载于:https://my.oschina.net/Bettyty/blog/747158

(--3198)2: Redirecting From A File( Piping and redirecting output相关推荐

  1. ISE中报错“Xst:442 - Invalid file extension (.v) for output format NGC‘

    Xst:442 - Invalid file extension (.v) for output format NGC 我仔细看了程序,发现是top module上面增加了.v,删去就好了. 只需要模 ...

  2. Eal:Error reading from file descriptor 33: Input/output error

    问题描述 VMWARE 虚机中,82545EM 虚拟网卡绑定 igb_uio 后,运行 dpdk 程序,dpdk 程序一直有如下报警信息: Eal:Error reading from file de ...

  3. Java File类总结和FileUtils类

    Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...

  4. new file https 找不到路径_Python3用pathlib模块替代os.path进行文件路径的操作

    本文使用 Zhihu On VSCode 创作并发布 在 Python 3.4 之前和路径相关操作函数都放在 os 模块里面,尤其是os.path这个子模块,可以说os.path模块非常常用.而在 P ...

  5. new file会创建文件吗_Rust 文件系统处理之文件读写 Rust 实践指南

    Rust 中,文件读写处理简单而高效.代码也很紧凑,容易阅读.我们从读取文件的字符串行.避免读取写入同一文件.使用内存映射随机访问文件这三个文件处理中的典型案例来了解一下. 文件处理场景大家都很熟悉, ...

  6. 边做边学:《 Inter Planetary File System》简介

    by Niharika Singh 由Niharika Singh 边做边学:< Inter Planetary File System>简介 (Learn by doing: a nic ...

  7. 打开Dev c++出现could not open language file English.lng

    原因:虽然叉掉后不影响运行,但每次都会提示,很烦.其实问题也简单,就是缺少English.lng文件. 解决方案:大家也不用下载,直接创建两个文本文档,把下面的代码复制进去,然后文档名及类型改为Eng ...

  8. (收藏)Turbo C 2.0、Borland C++库函数及用例

    字母A开头函数函数名: abort 功 能: 异常终止一个进程 用 法: void abort(void); 程序例: #include <stdio.h> #include <st ...

  9. Java 重定向 无法写入_java IO 文件读入,写入,重定向

    Java代码 packagestar20110526; importjava.io.BufferedInputStream; importjava.io.BufferedOutputStream; i ...

最新文章

  1. 电子科技大学技术交流报道
  2. 我学UML建模系列之核心元素 -------- 参与者
  3. 微信分享JSSDK-invalid signature签名错误的解决方案
  4. How to extend unallocated space to an existing partition on linux? | 如何在 linux 上扩展已有分区至未分配空间?
  5. .NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  6. Ubuntu下配置Window CIFS共享
  7. Comparable 与 Comparator
  8. python爬虫代码-学Python=写爬虫?不用代码也能爬下95%网站的数据!
  9. 研发项目wbs分解简单案例_做项目WBS(工作分解结构)
  10. 城市大脑总体框架和主要平台
  11. linux禁用笔记本键盘,禁用笔记本键盘 Linux
  12. onenote怎么同步到电脑_如何同步手机和电脑 onenote
  13. Springboot毕业设计毕设作品,心理评测系统 开题报告
  14. 完整的项目管理流程包括什么?
  15. Python 之 格式化输出
  16. bulk插入 es_ElasticSearch的Bulk操作 ES bulk详解
  17. K-hop消息传递图神经网络的表达能力有多强?
  18. r语言ggplot2 多线图绘制图例_R语言绘制箱线图示例
  19. Java——ArrayList(动态数组)介绍
  20. vi 和vim 的区别

热门文章

  1. GraphPad Prism:如何在轴上放置一个或多个缺口?
  2. Matlab | 空间域水印技术:LSB(Least Significant Bit):计算峰值信噪比PSNR(matlab源代码)
  3. 计算机视觉与深度学习 | 基于控制点的投影畸变图像配准(matlab源码)
  4. linux怎么删除mysql用户和组_linux下在mysql数据库中创建和删除用户
  5. python初中必背语法_初中必背英语语法知识汇总
  6. cdecl、stdcall、fastcall函数调用约定区别
  7. html游戏禁止微信浏览器下拉,如何用电脑模拟微信浏览器浏览禁止PC打开的微网站...
  8. STM32串口在首次发送字符的时候,首字符丢失解决办法
  9. 2021中超1 1010 zoto
  10. 【深度学习】保姆级教程,用PyTorch构建第一个神经网络