列表

详情


SHELL13. 去掉所有包含this的句子

描述

写一个 bash脚本以实现一个需求,去掉输入中含有this的语句,把不含this的语句输出
示例:
假设输入如下:
that is your bag
is this your bag?
to the degree or extent indicated.
there was a court case resulting from this incident
welcome to nowcoder


你的脚本获取以上输入应当输出:
that is your bag
to the degree or extent indicated.
welcome to nowcoder

说明:
你可以不用在意输出的格式,包括空格和换行

原站题解

Bash 解法, 执行用时: 2ms, 内存消耗: 424KB, 提交时间: 2022-01-30

while read line
do
if [[ $line =~ "this" ]]
then
continue
else
echo $line
fi
done < nowcoder.txt

Bash 解法, 执行用时: 2ms, 内存消耗: 428KB, 提交时间: 2022-02-06

# grep -v "this"

# sed '/this/d'
# sed '^/this/'


# awk '$0 !~ /this/ {print $0}'

# awk '!/this/ {print $0}'

while read line
do
if [[ $line =~ "this" ]]
then
continue
else
echo $line
fi
done < nowcoder.txt

Bash 解法, 执行用时: 2ms, 内存消耗: 460KB, 提交时间: 2021-03-05

#!/bin/bash
while read line
do
    flag=0
    for i in $line
    do
        if [[ $i = "this" ]];then
            flag=1
            break
        fi
    done
    if [[ $flag -eq 0 ]];then
        echo $line
    fi
done < nowcoder.txt

Bash 解法, 执行用时: 2ms, 内存消耗: 504KB, 提交时间: 2021-05-08

while read line
do
    j=0
    for i in $line
    do 
        if [[ $i = 'this' ]];then
           j=1
           break
        fi
    done
    if [[ j -eq 0 ]]
    then
        echo $line
    fi
done<./nowcoder.txt

Bash 解法, 执行用时: 2ms, 内存消耗: 504KB, 提交时间: 2021-05-03

#/bin/bash


while read line
do
    flag=0
    for  i in $line 
    do
        if [[ $i = "this" ]]
        then flag=1
         break
        fi
    done 
    
    if [[ $flag -eq 0 ]]
    then
        echo $line
    fi
done< nowcoder.txt

上一题