linux基礎(chǔ)2(字符管理)


grep

grep -o?root passwd? 匹配關(guān)鍵字體

grep -n root passwd?顯示行數(shù)

grep -i?root passwwd?忽略大小寫

grep -v?root?passwd?查找不包含root的內(nèi)容

grep -A 5?ntp?passwd?顯示ntp后5行

grep -B 5?ntp?passwd?顯示ntp前5行

grep -C 5 ntp passwd?顯示ntp前后5行

grep --color=auto? root passwd? 字體帶顏色

. *任意長度的字符

[ ] 匹配指定字符組內(nèi)的任一字符?

[^] 匹配不在指定字符組內(nèi)的任一字符

^[ ] 匹配以指定字符組內(nèi)的任一字符開頭?

^[^] 匹配不以指定字符組內(nèi)的任一字符開頭


grep -E (egrep)?擴展正則表達式

+ ?????????????匹配一個或多個前導(dǎo)字符

? ??????????????匹配零個或一個前導(dǎo)字符

a|b ???????????匹配a或b

( ) ?????????????組字符 hello myself yourself?? (my|your)self

x{m}? ???????前導(dǎo)字符x重復(fù)m次

[:alnum:]??????????字母與數(shù)字字符

[:alpha:] ????字母字符(包括大小寫字母)

[:blank:]? ???空格與制表符? ?????

[:digit:] | ???數(shù)字

[:lower:]??????小寫字母

[:upper:] ????大寫字母

[:punct:] ????標點符號?

[:space:]? ??包括換行符,回車等在內(nèi)的所有空白


vim /etc/bashrc?添加字符顏色

unalias grep?取消字體顏色

soure /etc/bashrc? 重新加載

grep ^# linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 以#號開頭的行

grep ^$ linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?空行

grep -v -e ^$? -e ^# linuxx? ??

grep -w root linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?root所在的行?并標識root

grep -v ^# linux | grep -v ^$

egrep -v '(^#|^$)' linux? ? ? ? ? ? ? ? ? ? ? 不顯示空行和帶#號開頭的行

grep "/<good" linux? ? ? ? ? ? ? ? ? ? ? ? ? ? good所在的行(goodb可以顯示)

grep "?/<good>/" linux? ? ? ? ? ? ? ? ? ? ? ?good所在的行(goob不能顯示)? 例最后一個? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 單詞為goodb

grep '/'^gmae linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?顯示^game?特殊符號顯示用‘/’

grep ens33$ linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?以ens33結(jié)尾的行

grep [o-z] linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?顯示出帶o-z字母的單詞

grep ^T? linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?標出T開頭的行

grep ^T+ linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 只顯示T開頭的行

grep '(^A|^a)' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?A a開頭的行


sed

sed '1d' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 刪除第一行

sed '1,3d' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?刪除1-3行

sed '2,$d' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 刪除2到末行

sed '/link/d' llinux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?刪除含link的行

(加-i? 徹底刪除)

sed '/ens33/p' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ens33的行 +1

sed '/^[Aa]pple/p' linux? ? ? ? ? ? ? ? ? ? ? ? ? ?[Aa]pple開頭的行 +1

sed -ne '1,2p' -e '/game/p' linux? ? ? ? ? ? ? 1-2行內(nèi)的含有g(shù)ame的行+1

sed -e '1,3d' -e 's/game/ok/p' linux? ? ? ? ?刪除1-3行?含有g(shù)ame的行+1?并把game改為ok

sed '2,/apple/p' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?第二到含apple開頭的行+1


sed -n '/ens33/p' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 只顯示含ens33的行

sed -n 's/game/123/p' linux? ? ? ? ? ? ? ? ? ? ? 只顯示game的行?并把game改為123

sed -n 's/game/123/gp' linux? ? ? ? ? ? ? ? ? ? 只顯示game的行?并把game改為123(整行替換)?

sed -n 's/game$/linux./p' linux? ? ? ? ? ? ? ? ? 只顯示game的行?以game結(jié)尾的后面加linux.

sed -n 's/game$/&.linux/p' linux? ? ? ? ? ? ? ?只顯示game的行? 在game后面加上.linux

sed 's/game$/&.linux/p' linux? ? ? ? ? ? ? ? ? ?顯示所有行?game行+1?并在game后面加.linux

sed 's/game$/&.linux/' linux? ? ? ? ? ? ? ? ? ? ? 顯示所有行?并在game后面加.linux

sed '/Apple/r io' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 把文件io的內(nèi)容全部寫到Apple的下一行

sed '/Apple/w io' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?把Apple整行的內(nèi)容寫到io中 (覆蓋)

sed '/game/a\---linux---' linux? ? ? ? ? ? ? ? ? 在game的下一行插入---linux---

sed 'y/game/GAME/p' linux? ? ? ? ? ? ? ? ? ? ? ?把字母game改成大寫GAME


awk

awk 'NR==4' linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 顯示第四行

awk '/game/' linux? ??(grep game linux)

awk /game/ linux? ? ? (grep -n '/game/p'? linx)? ? ??

? 顯示含有g(shù)ame的行

cat linux | grep -w game | awk '{print $2}'? ? 取含有g(shù)ame的行的第二列

awk ''/game/{print $2}' linux

awk -F " " 'NR==5{print $2}' linux? ? ? ? ? ? ? ? ?空格為分界符?取第五行的第二列

df -h | awk '{print $2}'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?取df-?h的第二列

df -h | awk '{print $3,$5}'? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?---------3 5列

date | awk '{print "Year:" $1 "\nMonth:" $2}'? ? ? Year:2018

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Moth: 09

cut

cut -d " " -f 2 linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?截取第二列

cut -d " " -f 1,3 linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?截取一三列

cut -d: -f1 passwd? 截取第一列d后面為分隔符

cut -d: -f1,3 passwd

cut -c1-6 passwd?前6個字符的列

cut -c6-12?passwd

cu -c6-?passwd? 6到最后


sort:將文件的每一行作為一個單位,從首字符向后,依次按ASCII碼值進行比較

-u :去除重復(fù)行

-r :降序排列,默認是升序

-o : 將排序結(jié)果輸出到文件中? 類似 重定向符號>

-n :以數(shù)字排序,默認是按字符排序

-t :分隔符

-k :第N列

-b :忽略前導(dǎo)空格。

-R :隨機排序,每次運行的結(jié)果均不同。

sort -nr -t: -k3 passwd?分隔符為:第三列?降序

sort -n linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?按1-11排序

sort -n -r linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 按11-1排序

sort -k 2 linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?第二列按a-z排序

sort -k 2 -r linux? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 第二列按z-a排序

sort?u?config? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 刪除重復(fù)行

sort -nu 1.txt -o 2.txt?重定向


tee工具

tee工具從標準輸入讀取并寫入標準輸出和文件,即:雙向覆蓋重定向<屏幕輸出|文本輸入>

-a 雙向追加重定向

echo?hello |?tee 1.txt? ?

echo world | tee -a 1.txt?追加

paste工具

paste工具用于合并文件行

-d:自定義間隔符,默認是tab

-s:串行處理,非并行


paste 1.txt 2.txt?合并

paste -d@ 1.txt 2.txt?文件間的分隔符為@

paste -s 1.txt 2.txt?串行處理


wc

wc -l /etc/passwd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 行數(shù)

wc -w /etc/passwd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?字數(shù)(單詞數(shù))

wc -c /etc/passwd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?字節(jié)數(shù)

wc -lwc /etc/passwd /etc/fstab? ? ? ? ? ? ? ? ? ? ? ? ? ? 行數(shù)字數(shù)字節(jié)數(shù)總量


find

find? / -name config? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?查找指定文件config的路徑

find? / -name config*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 查找所有帶config單詞的路徑 ? ? ? ??

find? /etc -name?ifcfg*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??查找etc下所有帶ifcfg單詞的路徑

find / -type?

b? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-塊設(shè)備

d? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-目錄

c? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -字符設(shè)備文件

p? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -管道文件

l? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-符號鏈接文件

f? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-普通文件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容