三劍客過濾長字符串
記一次過濾文件內(nèi)容,三劍客awk、grep、sed過濾指定字段、列后,怎料其中混雜編碼字符串,這不是我們想要的。所幸,找到了規(guī)律,那就是 它 很長…,直接干掉長字符串即可! 下邊是三把劍具體實現(xiàn)!
我有三把劍,一把awk,一把grep,一把sed
[root@centos]# cat test hello
helloword
test66
awk式
且看,統(tǒng)計字符串長度,用到招式 length 函數(shù)
[root@centos]# echo 'hello' | awk '{print length($1)}'5
如看官所愿,得到字符串的長度 5
連招,加 if 語句,輸出指定的字符串長度內(nèi)容小于等于6的
[root@centos]# awk '{ if ( length($0) <=6 ) print $0}' test hello
test66
grep式
[root@centos]# egrep -w '^.{1,6}' test hello
test66
egrep參數(shù):相當于 grep -E ,用于匹配正則
-w參數(shù):僅跟模式匹配的字符串
^. 參數(shù):表示以任意字符開頭
sed式
[root@centos]# sed -n '/^.\{7,\}/!p' test hello
test66
-n參數(shù):–silent,配合編輯命令只打印符合條件字符串
!p參數(shù):符合條件的不打印,p即為打印輸出
\參數(shù):轉(zhuǎn)義字符,轉(zhuǎn)義 { }
各位看官,江湖再會!
Sunny_Future
原創(chuàng)文章 149獲贊 338訪問量 28萬+