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/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393703.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393703.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393703.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

dreamweaver后綴名_讓 Dreamweaver 8 支持其它擴展名的方法

有的時候&#xff0c;我們為了網站的安全考慮&#xff0c;常常會修改網站的擴展名&#xff0c;如我看到有些網站的擴展名為 *.do&#xff0c;很明顯這個擴展名是有意改掉的&#xff0c;可是改了這擴展名對于我們修改網頁來說就麻煩了&#xff0c;比如用 Dreamweaver 8 來修改的…

sublime 3143 注冊碼

請大家支持購買正版&#xff0c;或者使用Atom、Vimsublime 3143版本的注冊碼&#xff1a;—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA12C0 A37081C5 D0316412 4584D136 94D7F7D4 95BC8C1C 527DA828 560B…

【BZOJ1857】【SCOI2010】傳送帶 [三分]

傳送帶 Time Limit: 1 Sec Memory Limit: 64 MB[Submit][Status][Discuss]Description 在一個2維平面上有兩條傳送帶&#xff0c;每一條傳送帶可以看成是一條線段。兩條傳送帶分別為線段AB和線段CD。lxhgww在AB上的移動速度為P&#xff0c;在CD上的移動速度為Q&#xff0c;在平…

google android廣告異步加載,谷歌廣告異步代碼和同步代碼的解決方法

通常大部分人初次接觸谷歌google adsense廣告聯盟都會有疑問&#xff0c;在新建單元界面我們可以看到獲取代碼類型選項。下面是學習啦小編為大家整理的關于谷歌廣告異步代碼和同步代碼的解決方法&#xff0c;一起來看看吧!谷歌廣告異步代碼和同步代碼的解決方法選擇同步還是異步…

openssl 加密解密 指令_Shell openssl命令加密解密字符串

Linux下的 openssl 命令解密我們以在線加密網站為例 http://tool.chacuo.net/cryptdes我們選擇des cbc模式&#xff0c;密鑰為abcdefgh&#xff0c; 偏移量為12345678&#xff0c;以base64輸出結果 對hello進行加密&#xff0c;得到結果8Snw/EmQdY我們再用將在線網站改用shell命…

使用Docker 安裝Elasticsearch、Elasticsearch-head、IK分詞器 和使用

使用Docker 安裝Elasticsearch、Elasticsearch-head、IK分詞器 和使用 原文:使用Docker 安裝Elasticsearch、Elasticsearch-head、IK分詞器 和使用Elasticsearch的安裝 一、elasticsearch的安裝 1.鏡像拉取 docker pull elasticsearch:tag2.啟動 docker run -it -e "disc…

Spring 的持久化實例(JDBC, JdbcTemplate、HibernateDaoSupport、JdbcDaoSupport、SqlSessionDaoSupport等)...

2019獨角獸企業重金招聘Python工程師標準>>> 一、表&#xff08;這里用mysql&#xff0c;數據庫名為yiibai&#xff09; CREATE TABLE customer (CUST_ID int(10) UNSIGNED NOT NULL,NAME varchar(100) NOT NULL,AGE int(10) UNSIGNED NOT NULL ) ENGINEInnoDB DEFA…

開始使用gradle

前提配置gradle環境 每個gradle構建都是以一個腳本開始的。gradle構建默認的名稱為build.gradle。當在shell中執行gradle命令時&#xff0c;gradle會去尋找為build.gradle文件&#xff0c;如果找不到就會顯示幫助信息。 下面我們以經典的helloworld為例。 1、首先建立一個build…

freecodecamp_freeCodeCamp的新編碼課程現已上線,其中包含1,400個編碼課程和6個開發人員認證

freecodecampFor the past year, our community has been hard at work on a massive new programming curriculum. And now that curriculum is live and out of beta!在過去的一年中&#xff0c;我們的社區一直在努力編寫大量的新編程課程。 現在&#xff0c;該課程已上線并且…

麥克勞林展開式_數學家麥克勞林與牛頓的故事

數學家麥克勞林麥克勞林(Colin Maclaurin1698年2月-1746年6月), 蘇格蘭數學家&#xff0c;麥克勞林是18世紀英國最具有影響的數學家之一。01麥克勞林是一位牧師的兒子&#xff0c;半歲喪父&#xff0c;9歲喪母。由其叔父撫養成人。叔父也是一位牧師。麥克勞林是一個“神童”&am…

html隱藏層點擊顯示不出來,[js+css]點擊隱藏層,點擊另外層不能隱藏原層

1貨幣轉換&#xff0c;下圖顯示了這個程序子只進行簡單的 把元素放在下面的目錄下&#xff0c;在創幣轉換應用程序這個例 所需的界面&#xff0c;包括一些UI組件實例(Button, ComboB 貨幣轉換&#xff0c;下圖顯示了這個程序組件實例(Button, ComboB 貨幣轉換&#xff0c;下圖顯…

Oracle 10.2.0.5 非歸檔current redolog損壞處理一例

操作系統: RHEL5.8 x64數據庫 : Oracle 10.2.0.5.0故障情況:一臺單機曙光PC服務器4塊300G SAS盤&#xff0c;RAID5壞兩塊磁盤&#xff08;服務器面板無故障提示&#xff0c;無人發現&#xff09;&#xff0c;造成RAID5磁盤陣列掛掉&#xff0c;操作系統當機&#xff0c;系統無…

基礎命令

date --help date %T 15:04:58 whatis date date (1) - print or set the system date and timeman date 獲取詳細的命令解釋cd ~/wntlab //新建文件夾 mkdir example //新建文件 touch b c //復制文本內容 cp b c//把 b的內容復制給 c cp b a/ //把 文件b復制…

微信小程序把玩(三十三)Record API

微信小程序把玩&#xff08;三十三&#xff09;Record API 原文:微信小程序把玩&#xff08;三十三&#xff09;Record API其實這個API也挺奇葩的&#xff0c;錄音結束后success不走&#xff0c;complete不走&#xff0c;fail也不走&#xff0c; 不知道是不是因為電腦測試的原因…

leetcode336. 回文對(字典樹)

給定一組 互不相同 的單詞&#xff0c; 找出所有不同 的索引對(i, j)&#xff0c;使得列表中的兩個單詞&#xff0c; words[i] words[j] &#xff0c;可拼接成回文串。 示例 1&#xff1a; 輸入&#xff1a;[“abcd”,“dcba”,“lls”,“s”,“sssll”] 輸出&#xff1a;[[…

html文檔 字符引用,【轉】HTML中常見形如#number;的東西叫做 字符實體引用,簡稱引用,代表一個對應的unicode字符...

【轉】HTML中常見形如number;的東西叫做 字符實體引用&#xff0c;簡稱引用&#xff0c;代表一個對應的unicode字符英文解釋的很清楚&#xff0c;就不翻譯了&#xff0c;自己看&#xff1a;EntitiesCharacter entity references, or entities for short, provide a method of e…

終端打開后-bash_如何爵士化Bash終端-帶有圖片的分步指南

終端打開后-bashby rajaraodv通過rajaraodv In this blog I’ll go over the steps to add Themes, Powerline, fonts, and powerline-gitstatus to make your regular Bash Terminal look beautiful and useful as shown in the picture above.在此博客中&#xff0c;我將介紹…

如何獲取元素在父級div里的位置_關于元素的浮動你了解多少

首先&#xff0c;在介紹什么是浮動之前我們先介紹一下html中元素的普通流布局方式。在普通流中&#xff0c;元素是按照它在 HTML 中的出現的先后順序自上而下依次排列布局的&#xff0c;在排列過程中所有的行內元素水平排列&#xff0c;直到當行被占滿然后換行&#xff0c;塊級…

獲取iOS頂部狀態欄和Navigation的高度

狀態欄的高度 20 [[UIApplication sharedApplication] statusBarFrame].size.height Navigation的高度 44 self.navigationController.navigationBar.frame.size.height 加起來一共是64 轉載于:https://www.cnblogs.com/Free-Thinker/p/6478715.html

Java電商項目-5.內容管理cms系統

目錄 實現加載內容分類樹功能實現內容分類動態添加刪除內容分類節點實現內容分類節點的分頁顯示實現廣告內容的添加實現廣告內容刪除實現廣告內容編輯到Github獲取源碼請點擊此處實現加載內容分類樹功能 注: 往后將不在說編寫遠程服務方法和編寫web模塊等重復語句, 直接用"…