首页 > 试题广场 >

去掉不需要的单词

[编程题]去掉不需要的单词
  • 热度指数:16157 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
写一个bash脚本以实现一个需求,去掉输入中含有B和b的单词
示例:
假设输入如下:
big
nowcoder
Betty
basic
test

你的脚本获取以上输入应当输出:
nowcoder test

说明:
你可以不用在意输出的格式,空格和换行都行
示例1

输入

big
nowcoder
Betty
basic
test

输出

nowcoder
test
text="big
nowcoder
Betty
basic
test"

# 使用awk
echo "$text" | awk '$0 !~/[Bb]/{print}'

# 使用sed
echo "$text" | sed -nr '/B|b/!p'

# 更绕的写法,仅得到包含这个字母的行号,删除对应行
de_line=$(echo "$text" | while read -r line;do
    let line_num++
    for ((i=0; i<${#line}; i++));do
        if [[ "${line:i:1}" == "b" || "${line:i:1}" == "B" ]];then
            echo $line_num
        fi
    done
done)
if [ -z $de_line ];then
    echo "$text"
else
    de_line=$(echo $de_line)
    de_line=${de_line// /d;}d
    echo "$text" | sed "$de_line"
fi
发表于 2025-10-23 09:57:20 回复(0)
#!/bin/bash
while read line
do
if [[ $line =~ [Bb] ]];then
    continue
else echo $line
fi
done < nowcoder.txt

发表于 2023-09-27 16:10:03 回复(0)
awk -F "[b,B]" '{if(NF == 1) print $0}'
发表于 2022-09-07 17:18:47 回复(0)
grep -v "[bB]" nowcoder.txt

awk '$0 !~ /[bB]/{print}' nowcoder.txt
发表于 2022-08-27 18:16:38 回复(0)
grep -v b nowcoder.txt | grep -v B
发表于 2022-08-23 20:43:53 回复(0)
grep -v "[Bb]"

发表于 2022-08-21 17:30:38 回复(0)
grep -E '\b[^bB]+\b' nowcoder.txt 
发表于 2022-07-13 15:02:50 回复(0)
grep -Ev "B|b" 
sed  '/[bB]/d' 
awk '!/[b|B]/{print $0}'
发表于 2022-07-12 19:48:05 回复(0)
grep -v [B,b]
发表于 2022-07-08 19:39:59 回复(0)
sed '/[bB]/d'
这也算难题?来搞笑的吧



发表于 2022-06-25 16:03:22 回复(1)
grep -v "B\|b" | tr '\n' ' '
发表于 2022-05-24 21:29:03 回复(0)
3种方法
$grep -v '[bB]'
$awk '$0!~/B|b/ {print $0}'
$sed '/[Bb]/d'

发表于 2022-05-19 21:23:34 回复(0)
grep -v 'B\|b'
发表于 2022-05-13 17:29:33 回复(0)
awk '$0!~/B|b/ {print $0}'
发表于 2022-05-10 12:36:38 回复(0)
sed '/[b,B]/d'

发表于 2022-05-02 14:40:48 回复(0)
awk  '!/[Bb]/ {ORS=" ";print $0}' nowcoder.txt
发表于 2022-04-30 22:57:21 回复(0)
grep -v ['B','b']
发表于 2022-04-27 02:46:53 回复(0)
grep -v -E 'b|B' nowcoder.txt | tr -s '\n' " "
发表于 2022-04-24 15:22:07 回复(0)