by Luciano Strika

通过卢西亚诺·斯特里卡(Luciano Strika)

Command Magicks:如何使用控制台处理文件和字符串 (Command Magicks: How to Manipulate Files and Strings with the Console)

As developers, there are lots of repetitive things we do every day that take away our precious time. Finding ways to automate and optimize those processes is usually very lucrative.

作为开发人员,我们每天都会做很多重复的事情,这会浪费我们宝贵的时间。 寻找使这些过程自动化和优化的方法通常非常有利可图。

Many times we find ourselves sifting through a program’s output looking for the relevant bits and manually moving them into a different file, changing all the capital letters from a sentence into lowercase, or removing all non-numerical characters from a file. The kind of boring, repetitive and error-prone tasks that can pile up if we do them by hand, and turn into a big headache.

很多时候,我们发现自己在程序的输出中进行筛选以寻找相关的位,然后将它们手动移到另一个文件中,将句子中的所有大写字母更改为小写,或从文件中删除所有非数字字符。 如果我们手工完成这些繁琐的,重复性的且容易出错的任务,这些任务会变得很头疼。

It’s conventional wisdom that we should do these things programmatically not manually. Many times the problem falls into this sweet spot where coding a whole script for it, even in Python, feels like overkill. Doing the thing by hand will take too long or generate too many errors.

按照惯例,我们应该以编程方式而不是手动方式执行这些操作。 很多时候问题都落在这个甜蜜的地方,即使用Python编写整个脚本,也感觉有些过头了。 用手做事情会花费太长时间或产生太多错误。

Luckily, many of those tasks have already been coded by people way smarter than we are. They can be accessed with just a few key presses. They are all available as shell commands, and I’ll show you some of them today. If you’re completely new to the terminal and have no idea how to navigate your file system or do similar tasks, I suggest you read my previous introduction to the terminal.

幸运的是,其中许多任务已经被人们以比我们聪明的方式进行了编码。 只需按几下即可访问它们。 它们都可以作为shell命令使用,今天我将向您展示其中的一些。 如果您不熟悉终端设备,又不知道如何导航文件系统或执行类似的任务,建议您阅读以前对终端设备的介绍 。

So without further ado, let me introduce you to the most useful spells any coding wizard should know.

因此,事不宜迟,让我向您介绍任何编码向导都应该知道的最有用的法术。

echo:使字符串出现在控制台中 (echo: Make a string appear in the console)

Before we can dive into the arts of divination and transformation, a real programming wizard must dominate the craft of conjuration.The echo command, followed by a string, will simply make the console output what was given as input. For instance, running the following line:

在我们深入研究除法和变换的技巧之前,必须先使用真正的编程向导来控制变魔术。echo命令后跟一个字符串,将使控制台输出简单地作为输入。 例如,运行以下行:

echo “hello world!”

will produce the following output:

将产生以下输出:

hello world!

This may seem trivial right now, but I promise it will be useful in the future.

现在这似乎微不足道,但我保证将来会有用。

cat:显示输入的真实格式 (cat: Showing the input’s true form)

Calling the cat command on a file will output its contents into the terminal.For instance, we have a directory containing the files ‘file1.txt’ and ‘file2.txt’. Both files contain the text ‘this is a file’. Calling:

在文件上调用cat命令会将其内容输出到终端中,例如,我们有一个包含文件'file1.txt'和'file2.txt'的目录。 两个文件都包含文本“这是一个文件”。 致电:

cat file1.txt

will output the file’s contents:

将输出文件的内容:

this is a file

Note that the argument for the cat command can be any shell style name. We can use the wildcard character *, to match any string. This way, we could output different files’ contents one after another, like this:

请注意,cat命令的参数可以是任何外壳样式名称。 我们可以使用通配符*来匹配任何字符串。 这样,我们可以一个接一个地输出不同文件的内容,如下所示:

cat *.txt

In this case, * matches both file1 and file2, and they both end in .txt, so they’re both printed. That command’s output would be

在这种情况下,*匹配file1和file2,并且都以.txt结尾,因此都被打印出来。 该命令的输出将是

this is a filethis is a file

Remember this command — no warlock is really complete without a kitten.

请记住此命令-没有小猫,没有术士真的是完整的。

grep:大海捞针 (grep: finding a needle in a haystack)

Switching to divination, grep is the spell for finding a substring in a string. Calling

切换到占卜,grep是在字符串中找到子字符串的咒语。 呼唤

grep <some string> filename

will output every line of the specified file where the given string appears.

将输出指定文件出现给定字符串的每一行。

If we wish for it to appear not only in its exact form but also with different casing, we must pass the -i argument, to ignore casing.

如果我们希望它不仅以确切的形式出现而且以不同的大小写形式出现,则必须传递-i参数以忽略大小写。

If we call it on different files in a single command, we will get a list of every file with lines matching the pattern. For instance in the previous directory, calling

如果在单个命令中在不同文件上调用它,我们将获得每个文件的列表,其中的行与模式匹配。 例如在上一个目录中,调用

grep “this” *.txt

will yield

将产生

file1.txt: this is a filetile2.txt: this is a file

sed:将字符串转换为另一个 (sed: transforming a string into another)

The sed command is a transmutation spell. It takes a file’s contents and turns them into different ones. There are many ways of using it. Some of which I confess to knowing little of. (If you’re reading this and think of some cool things sed does that I am not mentioning, please tell me in the comments, as I love to learn new tricks). One of the most common ones is replacing the parts of a string that match a pattern, with different strings.

sed命令是一个spell变法术。 它获取文件的内容并将其转换为不同的内容。 有很多使用方法。 我承认其中一些知识很少。 (如果您正在阅读此书,并且想到了sed确实没有提到的一些很棒的事情,请在评论中告诉我,因为我喜欢学习新的花样)。 最常见的一种方法是用不同的字符串替换与模式匹配的字符串部分。

This is done by calling

这是通过调用

sed “s/regexp/replacement/optional_flags” file_name

What this will do is:

这将是:

  • Look for every line that matches the regexp in the file_name file在file_name文件中查找与regexp匹配的每一行
  • Replace that line’s first regexp instance with replacement

    更换内容替换该行的第一个正则表达式实例

  • Output the resulting string into the console (without altering the file!).将结果字符串输出到控制台(无需更改文件!)。

If we supply the g flag at the end (like this s/old/new/g) it will match all instances on each line, instead of just the first one. Using the -i argument (for in-place) will actually write into the input file.

如果我们在末尾提供g标志(例如s / old / new / g),它将匹配每行的所有实例,而不仅仅是第一个。 使用-i参数(就地使用)实际上将写入输入文件。

As an example, calling

举例来说,

sed “s/is/was/g” file1.txt

will output

将输出

thwas was a file

If we want only to match entire words, we must put the \b character surrounding the regexp, like this

如果我们只想匹配整个单词,则必须将\ b字符放在正则表达式的周围,如下所示

sed “s/\bis\b/was/g” file1.txt

to finally get

最终得到

this was a file

结合我们的咒语:运算符 (Combining our spells: The Operators)

Now you’re proficient in four new schools of magic, each one with its characteristic spell. But to become a real wizard, you must learn to tie the threads of magic into awesome patterns. To do this, you will use three powerful tools.

现在,您已经精通了四门新的魔术学,每门都有其独特的法术。 但是,要成为一名真正的巫师,您必须学习将魔术的线绑入令人敬畏的模式。 为此,您将使用三个强大的工具。

| (管道)运算符 (| (Pipe) Operator)

The pipe operator takes the previous command’s output, and writes it into the following command’s input, creating a pipeline.For instance, calling

管道运算符获取上一条命令的输出,并将其写入到下一条命令的输入中,以创建管道。

cat *.txt | grep “is”

will first fetch the contents for all text files in the current working directory. Then look for every line that contains the string “is”, before finally printing them.

首先将获取当前工作目录中所有文本文件的内容。 然后,在最终打印它们之前,先查找包含字符串“ is”的每一行。

>(写)运算符 (> (write) Operator)

The write operator will write its input into its output — usually a file.

写操作符会将其输入写入输出中-通常是一个文件。

So for instance, a quick way of creating a text file with ‘this is a file’ as its contents, would be calling

因此,例如,一种以“这是一个文件”作为内容创建文本文件的快速方法是调用

echo “this is a file” > some_file.txt

See how that whole conjuring spell thing actually adds up? I told you it would be useful.

看到整个魔术咒语实际上是如何叠加的? 我告诉你这将是有用的。

Note that if the file already existed, this will overwrite its contents, without even asking. In case that’s not what we wanted, we must use our last tool:

请注意,如果文件已经存在,这将覆盖其内容,甚至不会询问。 如果这不是我们想要的,则必须使用最后一个工具:

>>(附加)运算符 (>> (append) Operator)

The >> operator will write its input into its output, except it won’t overwrite whatever’s already in it.

>>运算符会将其输入写入输出,除非它不会覆盖其中已有的内容。

That’s it, we’re through with this tutorial and you’re now a wizard’s apprentice. Go practice your new spellcasting skills, and you can thank me later. Do remember to check the man pages for all these commands if you get stuck or don’t remember what some flags did — a wizard’s never away from his books.

就是这样,我们已经完成了本教程,您现在是向导的学徒。 去练习您的新的施法技巧,稍后您可以感谢我。 如果您被卡住或不记得某些标志的作用,请记住检查所有这些命令的手册页-向导永远不会离开他的书。

Please consider supporting my writing habit with a small donation.

请考虑通过少量捐款来支持我的写作习惯 。

Follow me on Medium for more tutorials, tips and tricks. This article was also posted on datastuff.tech, my new website. Check it out!

在“ Medium”上关注我,以获取更多教程,技巧和窍门。 本文还发布在我的新网站datastuff.tech上。 看看这个!

翻译自: https://www.freecodecamp.org/news/command-magicks-how-to-manipulate-files-and-strings-with-the-console-3c554e64048/

Command Magicks:如何使用控制台处理文件和字符串相关推荐

  1. 在java读字符串入文件_如何在java中将文件读入字符串?

    我已经将文件读入String. 该文件包含各种名称,每行一个名称. 现在的问题是我想在String数组中使用这些名称. 为此我写了以下代码: String [] names = fileString. ...

  2. java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法...

    1.java代码中fastjson生成字符串和解析字符串的方法 List<TemplateFull> templateFulls = new ArrayList<TemplateFu ...

  3. 把XML文件转换为字符串

    后台管理跑马灯功能,用户添加完跑马灯图片之后,需要在后台产生一个xml文件,让前台直接读取这个xml文件即可.不必需要在前台显示时,再去循环跑马灯图片目录的所有图片. 前台读xml文件时,需要把xml ...

  4. bat递归查找指定文件_dos命令find图文教程,查找搜索文件文本字符串,bat批处理脚本...

    大家好,我是老盖,首先感谢观看本文,本篇文章做的有视频,视频讲述的比较详细,也可以看我发布的视频. 今天,我们学习DOS命令中的find这个命令,它这个命令可以查询文件的字符串,也可以查询另外其他命令 ...

  5. python查询文件软件_Python小工具--多文件查找字符串fs

    前几天,在看Android的makefile时,为方便自己在make文件查找字符串,用perl写了个小工具,实现的功能就是在目录下的多个文本文件中查找字符串.那个工具在同事的帮助下,已经实现了不少功能 ...

  6. linux find批量替换java文件中字符串

    linux批量替换java文件中字符串find ./ -name '*.java' | xargs perl -pi -e 's|MoonlightL|ZXL|g' find ./ -name '*. ...

  7. c语言中 文件的字符串输入函数是6,【C语言】文件操作及输入输出格式(文件常用的库函数)...

    参考中国大学MOOC 浙江大学翁恺C语言程序设计在线课程 目录 常用文件输入输出符号格式及标准 常用应用对象为文件的库函数简介: 库函数部分: 文件创建.打开.阅读: 数据块写入读出(只有这两个函数可 ...

  8. java getname_Java文件类字符串getName()方法(带示例)

    java getname 文件类字符串getName() (File Class String getName()) This method is available in package java. ...

  9. Java文件类字符串getAbsolutePath()方法(带示例)

    文件类字符串getAbsolutePath() (File Class String getAbsolutePath()) This method is available in package ja ...

最新文章

  1. Java数据访问对象模式
  2. 最全的CSS浏览器兼容问题整理
  3. linux配置jdk环境
  4. spring 注解说明以及@Resource和@Autowired的区别
  5. java连接mysql 5.7数据库_javaJDBC连接mysql(5.7)数据库,一看就懂的详细例子
  6. Activiti源码 之工厂模式
  7. 如何根据对象获取到对应的表名_Excel VBA 常用对象二
  8. C#随机不重复给数组赋值1-100并排序
  9. yii2 关系...
  10. CentOS 配置DHCPv6
  11. 2019最新《后盾网向军0基础学PHP教程》
  12. atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx...
  13. linux安装python教程视频_新手开发者的极简Linux上手Python视频教程
  14. 【考研经验】2018三跨哈工大深圳专硕初试复试经验
  15. 计网复习——传输层习题
  16. html弹窗广告文件怎么做,视频加入广告 如何在视频文件中加入弹窗广告
  17. Echarts引入省级地图(简便快捷,以浙江省为例)
  18. 超分辨率 | 综述!使用深度学习来实现图像超分辨率
  19. 浅尝辄止_数学建模(笔记_时间序列分析及其SPSS实现)
  20. 【推荐】智慧油田数字化油井智能入侵监测井口控制系统解决方案合集(共83份,884M)

热门文章

  1. [Python设计模式] 第17章 程序中的翻译官——适配器模式
  2. 程序2:word count
  3. Spark常见问题解决办法
  4. 关于meta便签详解
  5. Metro UI 菜单(Winform)
  6. android中的tabdemo
  7. 建议11: 区别对待==和Equals
  8. 搭建MSSM框架(Maven+Spring+Spring MVC+MyBatis)
  9. Docker 常用命令备忘录
  10. android baidupush