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

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值