一种情况下包含this单词,句中单词前后要有空格才符合条件,句首单词后有空格,前无空格,句尾单词前有空格后无空格;
还有一种就是只要包含this四个连续字符就算符合条件:一下都不是完美符合条件的,对于该题暂时够用======
- solution_1: grep -v 参数排除this,不以单词区分;
- solution_2: sed 工具正则删除包含'this'单词的行;
- solution_3: awk 工具正则输出包含'this'单词的行;
- solution_4: [[ ]] 双括号表达式中使用正则;
- solution_5,solution_6,solution_7: 每行的所有单词组成数组,对每个数组元素与'this'比较,包含匹配项则跳出数组遍历,查询下一行
#!/usr/bin/env bash
function solution_1() {
grep -v "this" nowcoder.txt
#cat nowcoder.txt | grep -v "this"
}
function solution_2() {
cat nowcoder.txt | sed '/\s\?this\s\?/d'
}
function solution_3() {
#cat nowcoder.txt | awk -F" " '{if(! ($0 ~ /\s?this\s?/)) print $0}'
awk -F" " '! ($0 ~ /\s?this\s?/) {print $0}' nowcoder.txt
}
function solution_4() {
while read line ; do
if [[ "${line}" =~ " this " ]]; then
continue;
fi
echo "${line}"
done < nowcoder.txt
}
function solution_5() {
local arr=""
while read line ; do
local result=0
arr=(${line})
for ele in ${arr[@]}; do
if [ "${ele}" = "this" ]; then
result=1
break
fi
done
if [ ${result} -eq 0 ]; then
echo "${line}"
fi
done < nowcoder.txt
}
function solution_6() {
local arr=""
while read line ; do
local result=0
arr=(${line})
for ele in ${arr[@]}; do
if [ "${ele}" = "this" ]; then
result=1
break
fi
done
if [ ${result} -eq 1 ]; then
continue
fi
echo "${line}"
done < nowcoder.txt
}
function solution_7() {
local arr=""
while read line ; do
local result=0
arr=(${line})
for ele in ${arr[@]}; do
if [ "${ele}" = "this" ]; then
result=1
break
fi
done
if ((${result} == 0)); then
echo "${line}"
fi
done < nowcoder.txt
}
solution_4

京公网安备 11010502036488号