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指令
1. echo "chenyu ni hao ya" | tr -d 'y'
2. chenu ni hao a
2)用sed指令
1. echo "hello*word*word" | sed 's/*//g'
2. 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/bash
value="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";
then
echo "first success"
else
echo "first fail"
fi
if echo $value | grep -q "hao";
then
echo "second success"
else
echo "second fail"
fi
允許結果如下:
1. ./grep.sh
2. 0
3. 1
4. first fail
5. second success
5 grep -i(忽略大小寫)來判斷結果
#/bin/bash
value="chenyu"
echo $value | grep -i "chen" > /dev/null
if [ $? -eq 0 ];
then
echo "grep success"
else
echo "grep fail"
fi
echo $value | grep -i "chengongyu" > /dev/null
if [ $? -eq 0 ];
then
echo "grep success"
else
echo "grep fail"
fi
運作結果
1. grep success
2. grep fail