linux shell之字符串的更具字符分割和删除字符和文本内容的删除以及内容是否匹配成功
生活随笔
收集整理的這篇文章主要介紹了
linux shell之字符串的更具字符分割和删除字符和文本内容的删除以及内容是否匹配成功
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 ?字符串的更具字符分割
1) xargs分割
echo "chenyu*hello*word" | xargs -d "*" chenyu hello word?
2)?awk分割
echo "chenyu*hello*word" | awk -F "*" '{print $1}' chenyu?
?
?
?
?
2?字符串的刪除字符
1)用tr命令
echo "chenyu ni hao ya" | tr -d 'y' chenu ni hao a2)用sed命令
echo "hello*word*word" | sed 's/*//g' hellowordword?
?
?
?
?
?
?
?
3 文本內容的刪除
用sed命令
cat 1.txt chenyu nihao hello word chenyu nihao dongma sed -i '/chenyu/d' 1.txt cat 1.txt nihao hello word nihao dongma?
?
?
?
?
?
?
?
?
?
4?grep -q 用于if邏輯判斷
-q 參數,本意是 Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. ?中文意思為,安靜模式,不打印任何標準輸出。如果有匹配的內容則立即返回狀態值0
自己測試如下
#/bin/bashvalue="chenyu ni hao ya"#匹配成功執行$?返回0 echo "hello" | grep -q "he"; echo $? #匹配失敗執行$?非0 echo "hello" | grep -q "hehelodaf"; echo $?#如果能匹配成功那么就到then反之到else if echo $value | grep -q "chenyuddsa"; thenecho "first success" elseecho "first fail" fiif echo $value | grep -q "hao"; thenecho "second success" elseecho "second fail" fi允許結果如下:
./grep.sh 0 1 first fail second success?
?
?
?
?
?
?
5 grep -i(忽略大小寫)來判斷結果
#/bin/bashvalue="chenyu" echo $value | grep -i "chen" > /dev/nullif [ $? -eq 0 ]; thenecho "grep success" elseecho "grep fail" fiecho $value | grep -i "chengongyu" > /dev/nullif [ $? -eq 0 ]; thenecho "grep success" elseecho "grep fail" fi運行結果
grep success grep fail?
總結
以上是生活随笔為你收集整理的linux shell之字符串的更具字符分割和删除字符和文本内容的删除以及内容是否匹配成功的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux shell之paste合并文
- 下一篇: NetBeans安装提示neatbean