采用的方式是在ubuntu虛擬機中交叉編譯,生成win10下的可執行程序grep.
ubuntu版本22.04
apt源為華為鏡像源
#安裝grep的依賴包
sudo apt install build-essential libpcre3-dev
sudo apt update
#這個是linux交差編譯到win上的工具鏈
sudo apt install mingw-w64
sudo apt install make
下載grep源碼包 版本3.6 Index of /gnu/grep
tar -xvf ./grep-3.6.tar.gz
cd grep-3.6
mkdir compile-win
cd compile-win
../configure --host=x86_64-w64-mingw32 --enable-threads=windows CC=x86_64-w64-mingw32-gcc
make
cd src
ls
dfasearch.o egrep fgrep grep.exe grep.o kwsearch.o kwset.o Makefile searchutils.o
這個時候編譯完成 但是grep.exe 體積還很大 接下來需要瘦身
strip --strip-all grep.exe
接下來把grep.exe copy到win10下
UPX: the Ultimate Packer for eXecutables - Homepage
下載upx壓縮工具
在cmd命令行中執行
upx.exe grep.exe
可以看到大小在一個滿意的范圍
測試一下
完成
請關注本頭條號,每天堅持更新原創干貨技術文章。
如需學習視頻,請在微信搜索公眾號“智傳網優”直接開始自助視頻學習
grep命令用于在文件中查找指定內容。本教程展示了一些最常見的grep命令示例,對軟件開發人員特別有益。最近,我開始使用Asciidoctor.js、Asciidoctor.js-pug、Asciidoctor-templates.js項目。當您第一次深入到包含數千行代碼的代碼庫時,高效工作并容易。但是我的秘密武器是grep。
我將通過示例與您分享如何在Linux中使用grep命令。
面向開發人員的10個grep命令實用示例
在Linux系統上使用grep命令
如果您查看man手冊,您將看到grep工具的簡短描述:
打印匹配語法的行。但是,不要被這種簡單的定義所愚弄
grep是Unix/Linux工具箱中最有用的工具之一,在處理文本文件時,有無數場合可以使用它。
最好是有真實的例子來學習它是如何工作的。因此,我將使用Asciidoctor.js源代碼樹來演示一些grep功能。您可以從GitHub下載源代碼樹,如果您愿意,您甚至可以查看我在撰寫本文時使用的同一個版本。這將確保您獲得的實驗結果與本文其余部分描述的完全相同:
git clone https://github.com/asciidoctor/asciidoctor.js
cd asciidoctor.js
git checkout v1.5.6-rc.1
Asciidoctor.js支持JAVA平臺的Nashorn JavaScript engine,我不了解Nashorn,因此我可以利用這個機會通過研究引用該JavaScript引擎的項目部分來進一步了解它。
查找所有出現的字符串(基本用法)
首先,我需要檢查package.json這個文件中是否有與Nashorn依賴關系的相關描述。
sh$ grep nashorn package.json
"test": "node npm/test/builder.js && node npm/test/unsupported-features.js && node npm/test/jasmine-browser.js && node npm/test/jasmine-browser-min.js && node npm/test/jasmine-node.js && node npm/test/jasmine-webpack.js && npm run test:karmaBrowserify && npm run test:karmaRequirejs && node npm/test/nashorn.js",
是的,顯然有一些針對Nashorn-specific的測試。讓我們進一步研究一下。
現在,我想仔細查看一下./npm/test/目錄中的文件,其中明確地提到了Nashorn。這里使用不區分大小寫的搜索(i選項)可能更好,因為我需要找到對nashorn和nashorn的引用(或任何其他大小寫字符的組合):
sh$ grep -i nashorn npm/test/*.js
npm/test/nashorn.js:const nashornModule = require('../module/nashorn');
npm/test/nashorn.js:log.task('Nashorn');
npm/test/nashorn.js:nashornModule.nashornRun('jdk1.8.0');
事實上,忽略大小寫在這里是有用的。否則,我就會錯過require('../module/nashorn')這樣的語句。毫無疑問,我應該稍后更詳細地檢查該文件。
npm/test/目錄中是否有一些非Nashorm相關的文件?為了回答這個問題,我們可以使用grep (-L選項)中的“打印非匹配文件”選項:
找到非匹配的文件
sh$ grep -iL nashorn npm/test/*
npm/test/builder.js
npm/test/jasmine-browser-min.js
npm/test/jasmine-browser.js
npm/test/jasmine-node.js
npm/test/jasmine-webpack.js
npm/test/unsupported-features.js
注意如何使用-L選項將grep的輸出結果更改為只顯示文件名。因此,上面的文件中沒有一個包含字符串nashorn(不管是哪種情況)。這并不意味著它們與這項技術沒有關系,但至少,字母nashorn不存在。
最后兩個命令使用shell glob模式將要檢查的文件列表傳遞給grep命令。但是,這有一些固有的限制:星號*將不匹配隱藏文件,它也不會匹配子目錄中包含的文件。
解決方案是將grep與find命令相結合,而不是依賴于shell glob語法:
# This is not efficient as it will spawn a new grep process for each file這是低效率的,因為它將為每個文件生成一個新的grep進程
$ find npm/test/ -type f -exec grep -iL nashorn \{} \;
# This may have issues with filenames containing space-like characters這可能與包含空格字符的文件名有關
grep -iL nashorn $(find npm/test/ -type f)
正如我在上面的代碼塊注釋中提到的,每個解決方案都有缺點。關于包含空格類型字符的文件名,可以研究一下grep -z選項,它與find命令的-print0選項結合使用,可以緩解這個問題。
然而,更好的解決方案是使用grep的“遞歸”-r選項。有了這個選項,您可以在命令行上給出搜索樹的根(開始目錄),而不是要檢查的文件名的顯式列表。使用-r選項,grep將檢查搜索目錄中的所有文件,包括隱藏的文件,然后遞歸深入到任何子目錄:
grep -irL nashorn npm/test/npm/
npm/test/builder.js
npm/test/jasmine-browser-min.js
npm/test/jasmine-browser.js
npm/test/jasmine-node.js
npm/test/jasmine-webpack.js
npm/test/unsupported-features.js
所以,在這個項目中似乎有一些Nashorn具體測試。由于Nashorn是Java,另一個可能提出的問題是“項目中是否有一些Java源文件明確提到了Nashorn?”
根據當前使用的grep版本,至少有兩個解決方案可以回答這個問題。第一個是使用grep查找包含nashorn的所有文件,然后將第一個命令的輸出重定向到第二個grep實例中,過濾掉非java源文件:
sh $grep -ir nashorn ./ | grep "^[^:]*\.java"
./spec/nashorn/AsciidoctorConvertWithNashorn.java:public class AsciidoctorConvertWithNashorn {
./spec/nashorn/AsciidoctorConvertWithNashorn.java: ScriptEngine engine = engineManager.getEngineByName("nashorn");
./spec/nashorn/AsciidoctorConvertWithNashorn.java: engine.eval(new FileReader("./spec/nashorn/asciidoctor-convert.js"));
./spec/nashorn/BasicJavascriptWithNashorn.java:public class BasicJavascriptWithNashorn {
./spec/nashorn/BasicJavascriptWithNashorn.java: ScriptEngine engine = engineManager.getEngineByName("nashorn");
./spec/nashorn/BasicJavascriptWithNashorn.java: engine.eval(new FileReader("./spec/nashorn/basic.js"));
現在應該可以理解命令的前半部分了。但怎么理解“^[\^:]*\.java”這部分呢?
除非指定-F選項,否則grep假設搜索模式是一個正則表達式。這意味著,除了與逐字匹配的普通字符之外,您還可以訪問一組元字符來描述更復雜的模式。我上面使用的模式將只匹配:
在本實例中,由于grep將使用冒號將文件名與上下文分隔開,因此在filename部分中只保留具有.java的行。值得一提的是,它還可以匹配.javascript文件名。
遞歸搜索帶nashorn關鍵內容的文件,忽略大小寫,并且文件名以.java結尾。
sh$ grep -ir nashorn ./ --include='*.java'
./spec/nashorn/AsciidoctorConvertWithNashorn.java:public class AsciidoctorConvertWithNashorn {
./spec/nashorn/AsciidoctorConvertWithNashorn.java: ScriptEngine engine = engineManager.getEngineByName("nashorn");
./spec/nashorn/AsciidoctorConvertWithNashorn.java: engine.eval(new FileReader("./spec/nashorn/asciidoctor-convert.js"));
./spec/nashorn/BasicJavascriptWithNashorn.java:public class BasicJavascriptWithNashorn {
./spec/nashorn/BasicJavascriptWithNashorn.java: ScriptEngine engine = engineManager.getEngineByName("nashorn");
./spec/nashorn/BasicJavascriptWithNashorn.java: engine.eval(new FileReader("./spec/nashorn/basic.js"));
精確匹配關鍵字-w選項,i忽略大小寫,指定文件后綴*.js,搜索關鍵字opal。
sh$ grep -irw --include='*.js' Opal .
選項--color為匹配選項著色。
sh $grep -irw --color=auto --include='*.js' Opal .
為匹配內容統計行數:
sh$ grep -irw --include='*.js' Opal . | wc -l
86
這意味著我們在所有檢查的文件中總共有86行匹配的行。然而,有多少不同的文件是匹配的?使用-l選項,您可以限制grep輸出匹配的文件的數量,而不是顯示匹配的行數。因此,這個簡單的改變將告訴有多少文件是匹配的:
sh$ grep -irwl --include='*.js' Opal . | wc -l
20
如果這讓您想起了-L選項,那么毫無疑問:因為它比較常見,所以小寫/大寫用于區分相反作用的選項。-l顯示匹配的文件名。-L顯示不匹配的文件名。另一個例子,我讓您檢查手冊中的-h和-h選項。
選項-c統計匹配的次數。
grep -irwc --include='*.js' Opal .
grep命令參數一覽
本文已同步至博客站,尊重原創,轉載時請在正文中附帶以下鏈接:https://www.linuxrumen.com/cyml/1690.html
點擊了解更多,快速查看更多的技術文章列表。