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/