[Linux]常用命令之【cat/echo/iconv/vi/grep/find/head/tail】


cat

cat [選項] [文件].. 
  # 一次顯示整個文件或從鍵盤創建一個文件或將幾個文件合並成一個文件

cat -n file1
  # 編號文件內容再輸出

echo

-n  輸出后不換行
-e  遇到轉義字符特殊處理  
   # eg:
       echo "he\nhe"   # 顯示he\nhe
       ehco -e "he\nhe"    # 顯示he(換行了)he

iconv

當前文件編碼為gb2312(from),轉換為utf-8(to)

iconv -f gb2312 -t utf-8 inputfilename > outputfilename

VI

0 vi中查找字符內容

  • step1 命令模式下輸入/字符串。例如:/Section 3

/ 向后搜索
? 向前搜索

  • step2 如果查找上/下一個,按N/n即可
要自當前光標位置向上搜索,請使用以下命令: /pattern Enter

其中,pattern表示要搜索的特定字符序列。

要自當前光標位置向下搜索,請使用以下命令: ?pattern Enter

1 按行號顯示文件文本

替代性方案: cat -n 或 nl

開啟行號
      ESC鍵 + 輸入(: set number 或者 : set nu)
關閉行號
      ESC鍵 + 輸入(: set nonumber)

2 跳到指定行

:$ 跳到文件最后一行
:0 或 :1 跳到文件第一行

3 文件內全局替換字符串

:%s/源字符串/目標字符串/g     【:%s/str1/str2/  替換文本內每一行中所有str1為str2】

替換文本的其它方法:

find $TOMCAT_HOME/webapps/ -name jdbc.properties | xargs sed -i 's/53302/3306/g'

其他操作

恢復vi編輯時不正常退出的文件(將會自動生成.swp隱藏文件)
    vi -r <fileName>
    :wq
    rm -f <fileName.swp>
:d 清空當前文件,后保存(:w)即可
    (等效於 cat /dev/null > file_name 或 echo "" > file_name【此法會在文件中寫入1個空行“\n"】)
:w 保存當前文件
:w filename 保存當前文件(如果進入 vim 的時候沒有指定要編輯的文件名,需要在保存文件的時候加上文件名 filename,如果進入 vim 的時候指定了 文件名,那么該用法相當於“另存為”)

:q 退出當前正在編輯的文件
:q! 強制退出當前正在編輯的文件並放棄最近一次保存到現在的所有操作
:wq 保存文件並退出

u 撤銷最近一次操作。(按 Ctrl+r 恢復撤銷掉的操作)
i 在光標所在的位置前面插入字符
a 在光標所在的位置后面插入字符
o 在光標所在行的下一行插入新的一行
O 在光標所在行的上一行插入新的一行
x 剪切光標處所在的字符。(x 前可先按一個數字,則剪切若干個字符)
dd 剪切光標處所在的一行。(dd 前可先按一個數字,則剪切若干行)
yy 復制光標處所在的一行。(yy 前可先按一個數字,則復制若干行)

:數字 跳轉到指定行(:18 跳轉到18行; :$ 跳到最后1行)
p 將剪切板中的資料粘貼到光標所在處
r 修改光標所在的字符,r 之后接跟你要修正的字符(比如你要把 fox 中 的 o 改成 i,只需將光標停在 o 上,接着連續按下 r 和 i)
h 將光標向前移動一個字符
j 將光標向下移動一個字符
k 將光標向上移動一個字符
l(小寫的 L)將光標向后移動一個字符
gg 跳到文本的最初一行
G 跳到文本的最末一行

Ctrl + u 向上(up)翻頁
Ctrl + d 向下(down)翻頁

:%s/old/new 將文件中所有的 old 字符串替換成 new

/string 從光標處往下查找字符串 string,注意在輸完你要查找的字符串 string。之后要按回車鍵。如果你要找的字符串 string 有多個,你可以按 n 將 光標跳到下一個位置,按 N 將光標跳到上一個位置

?string 跟上面的</string>是一樣的,區別是它從光標處往上查找

grep

grep亦支持正則搜索,可參見: Linux命令- grep +正則表達式[推薦] - CSDN
grep [-acinv] [--color=auto] '搜尋字符串bai' filename
選項/參數:
-a :將 binary 文件以 text 文件的方式搜尋數du據
-c :計算找到zhi '搜尋字符串' 的次數
-i :忽略大小寫的不同,所以大小寫視為相同
-n :順便輸出行號
-v :反向選擇,亦即顯示出沒有 '搜尋字符串' 內容的那一行
-m <-num-> / --max-count=<-num-> : 查看前N次出現的目標內容
-o, --only-matching : 只顯示一行中匹配PATTERN 的部分
-r 或 --recursive : 此參數的效果和指定"-d recurse"參數相同
-H, --with-filename print the file name for each match
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
--color=auto :【搜索的文本 字體高亮】;可以將找到的關鍵詞部分加上顏色的顯示喔!

  • 遞歸的-R、逐行/顯示行號的-n、在指定路徑<dirPath>下的文本內查找關鍵字keywords
# grep -nR "keyword" <dirPath> 
  • 【高亮色彩--color=auto】顯示指定文件集-name "*.sql"內(【忽略大小寫】-i)出現指定關鍵字的情況,需:【顯示文件名-H】、【顯示行號-n
$ find ./ -name "etl_task_resource*.sql" | xargs grep -Hni --color=auto 'create database'
./etl_task_resource-data.mysql.10.0.11.138.202104012104.bak.sql:22:CREATE DATABASE /*!32312 IF NOT EXISTS*/ `ETL_TASK_SOURCE_234` /*!40100 DEFAULT CHARACTER SET utf8 */;
  • 列出在當前路徑下查找出現指定關鍵詞的文件名
[root@localhost opt]# grep -i apple  -rl --include="*.out"
output.out

[root@sdc04 opt]# grep -i apple  -rl --include="*.out" | xargs cat
hello world 
apple tree follow 
test-demo.txt 2435
  • 統計 指定目錄下的文件/文件夾個數
ls -l | grep "^-" | wc -l
  # 統計文件個數

ls -l | grep "^d" | wc -l
  # 統計文件夾個數
  • 查看文件中關鍵詞出現的次數
grep -o 'keyword' <fileName> | wc -l

wc -l : 統計次數

  • 查看前N次出現的目標內容

grep -m <-num-> <-fileName-> = grep --max-count=<-num-> <-fileName->

Stop reading the file after num matches.

(尤其在查看Tomcat日志時,需要查看第一次出現異常在什么時間 (防止無故背鍋!!!) 非常有效!!!)
注:亦可結合head方法使用,詳見本文:head
---- 2020-10-17 12:16

[root@govern ~]# cat /xx/xx/xxdataxxalxxy/logs/catalina.out | grep -m 1  -C 100 'KettleDatabaseException'
29-Sep-2020 13:20:29.308 信息 [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一個JAR被掃描用於TLD但尚未包含TLD。 為此記錄器啟用調試日志記錄,以獲取已掃描但未在其中找到TLD的完整JAR列表。 在掃描期間跳過不需要的JAR可以縮短啟動時間和JSP編譯時間。
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
加載Etl資源倉庫管理構造方法
PluginFilesFolder:plugins/logging
PluginFilesFolder:/home/sdc/.kettle/plugins/logging
PluginXmlFolder:plugins
PluginXmlFolder:plugins/valuemeta
PluginFilesFolder:plugins/valuemeta
PluginXmlFolder:/home/sdc/.kettle/plugins
PluginXmlFolder:/home/sdc/.kettle/plugins/valuemeta
PluginFilesFolder:/home/sdc/.kettle/plugins/valuemeta
PluginXmlFolder:/home/sdc/wydataquality/webapps/wydataquality/WEB-INF/classes//plugins
PluginFilesFolder:plugins/passwordencoder
PluginFilesFolder:/home/sdc/.kettle/plugins/passwordencoder
PluginFilesFolder:plugins/logging
PluginFilesFolder:/home/sdc/.kettle/plugins/logging
PluginXmlFolder:plugins
PluginXmlFolder:plugins/valuemeta
PluginFilesFolder:plugins/valuemeta
PluginXmlFolder:/home/sdc/.kettle/plugins

...

Error connecting to the repository!

Error occurred while trying to connect to the database

驅動程序類'org.gjt.mm.mysql.Driver'無法找到,請確保 'MySQL' 驅動程序(jar文件)已安裝.
org.gjt.mm.mysql.Driver
	at org.pentaho.di.repository.kdr.delegates.KettleDatabaseRepositoryConnectionDelegate.connect(KettleDatabaseRepositoryConnectionDelegate.java:160)
	at org.pentaho.di.repository.kdr.KettleDatabaseRepository.connect(KettleDatabaseRepository.java:193)
	at org.pentaho.di.repository.kdr.KettleDatabaseRepository.connect(KettleDatabaseRepository.java:182)
	at com.cdsf.common.objectPool.factory.ETLRepositoryFactory.createNew(ETLRepositoryFactory.java:78)

...

	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.pentaho.di.core.exception.KettleDatabaseException: 
Error occurred while trying to connect to the database

驅動程序類'org.gjt.mm.mysql.Driver'無法找到,請確保 'MySQL' 驅動程序(jar文件)已安裝.
org.gjt.mm.mysql.Driver

	at org.pentaho.di.core.database.Database.normalConnect(Database.java:432)
	at org.pentaho.di.core.database.Database.connect(Database.java:325)
	at org.pentaho.di.core.database.Database.connect(Database.java:296)
	at org.pentaho.di.core.database.Database.connect(Database.java:284)
	at org.pentaho.di.repository.kdr.delegates.KettleDatabaseRepositoryConnectionDelegate.connect(KettleDatabaseRepositoryConnectionDelegate.java:142)
	... 54 more

此 grep + head的組合命令亦有效:

grep -C 200 'KettleDatabaseException' /xx/yy/wmmdzzzquality/logs/catalina.out | head -200
  • 最后1次出現的目標內容
    (大文件,性能低)
grep -C 200 'KettleDatabaseException' /xx/yy/wmmdzzzquality/logs/catalina.out | tail -200
  • 匹配多個關鍵字

grep -E "word1|word2|word3" file.txt
匹配 file.txt 中包含 word1 或 word2 或 word3 的行。
滿足其中任意條件(word1、word2和word3之一)就會匹配。

ps -ef | grep apusic | grep -E "/opt/govern/wydataeye| /opt/govern/datasource | /opt/govern/wydataquality| /opt/govern/wytaskwatcher"
  • 查看目標搜索內容的上下N行

grep -C N [ ] 顯示filename文件中,targetContentStr行上下N行內容(含targetContentStr行)

grep -C 3 love filename  顯示filename文件中,love行【上下】3行內容(含love行)

grep -A 3 love filename  顯示filename文件中,love行【下】3行內容(含love行)

grep -B 3 love filename  顯示filename文件中,love行【上】3行內容(含love行)

find

  • 列出目標路徑下,文本內含有指定字符串的文件
find <targetDirPath> -type f | xargs  grep -Hn --color=auto "<key-word>" 

find <targetDirPath> -name "*.json" | xargs grep <key-word>   # 查找所有含有<key-word>關鍵詞的<json>文件

find <targetDirPath> -user johnny | xargs  grep -Hn --color=auto "<key-word>" 
  # 查找所有含有<key-word>關鍵詞的、且屬於用戶johnny的文件

(示例:↓)

[root@sdc228 ~]# find /var/ftp/www/share-portal-biz/ -name "*.*" | xargs grep -i discovery-server.sefon.com
/var/ftp/www/share-portal-biz/conf/bootstrap.yml:        server-addr: discovery-server.sefon.com:57102
/var/ftp/www/share-portal-biz/conf/bootstrap.yml:        server-addr: discovery-server.sefon.com:57102
  • 列出目標路徑下,文件名含有指定字符的文件
ll <targetDirPath> | grep "<key-word>" 
或
find <targetDirPath> -name "*<key-word>*" 
  • 替換文本內容
find <targetDirPath> -name *.properties |xargs sed -i 's/old_str/new_str/g' # 替換<targetDirPath>目錄下所有名為.properties的文件內字符串old_str為new_str

head

  • 基本用法
[root@linux ~]# head /etc/passwd   #默認,顯示開頭前10行
 
[root@linux ~]# head -10 /etc/passwd   #顯示開頭前10行

[root@linux ~]# head -q -n 10 file1 file2 file3   #顯示多文件開頭前10行,並且不顯示文件名的文件頭

[root@linux ~]# head -n -10 /etc/passwd   #除最后10行外,顯示剩余全部內容。
  • 查看首次出現的目標內容
    在出現【關鍵詞(KettleDatabaseException)】的前后(200)行的查詢結果中,查看首次出現目標內容塊的前(200)行
grep -C 200 'KettleDatabaseException' /xx/yy/zzztaquality/logs/catalina.out | head -200

tail

  • 查看文件前100行
cat filename | head -n 100
  • 查看文件后50行
cat filename | tail -n 50
  • 從1000行開始顯示 / 顯示1000行以后的
tail -n +1000
  • 顯示1000行到3000行內容
cat filename |head -n 3000 | tail -n +1000

直接用sed命令: sed -n '5,10p' filename # 只查看文件的第5行到第10行

  • 從第3000行開始,顯示1000行

即 顯示 第3000~3999行

cat filename | tail -n +3000 | head -n 1000

參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM