首页 > 试题广场 >

去掉不需要的单词

[编程题]去掉不需要的单词
  • 热度指数: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
sed "/[b,B]/d" nowcoder.txt
发表于 2021-03-01 11:43:30 回复(0)
cat nowcoder.txt|grep -vi "b"
发表于 2021-01-06 16:50:33 回复(0)
全套

grep -v -E 'b|B' nowcoder.txt
grep -iv "b"
grep -v '[bB]' 

cat nowcoder.txt | grep -v -E 'b|B' 
cat nowcoder.txt|grep -vi "b"

awk '$0!~/b|B/ {print $0}' nowcoder.txt
awk '!/[bB]/'


sed '/[Bb]/d'
sed '/b\|B/d'


编辑于 2021-04-10 11:21:59 回复(3)
grep -iv "b"
发表于 2021-01-28 15:51:53 回复(0)
grep -v [Bb]
发表于 2021-06-10 21:53:03 回复(0)
cat  $1 |grep -v -i b



编辑于 2020-11-12 19:43:00 回复(3)
崯头像
grep -v [Bb]
发表于 2024-11-19 03:33:56 回复(0)
awk '$0!~/[b|B]/{print $0}' nowcoder.txt
发表于 2021-04-13 14:33:10 回复(0)
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)
grep -v -i 'b'
发表于 2025-08-05 16:35:29 回复(0)
file="nowcoder.txt"
cat $file | sed '/[b,B]/d'
发表于 2024-07-03 15:58:15 回复(0)
```
#!/bin/bash

while IFS= read -r line; do
[[ $line =~ [bB] ]] || echo "$line"
done
```
发表于 2024-07-02 15:59:26 回复(0)
awk '
    {
        flag=0
        for(i=1;i<=length($1);i++){
            if(substr($1,i,1)=="B"||substr($1,i,1)=="b"){
                flag=1
                break
            }
        }
        if(flag==0){
            print $0
        }
    }
    '
发表于 2024-06-30 15:26:56 回复(0)
awk '!/B|b/' nowcoder.txt

发表于 2024-06-23 22:21:38 回复(0)
awk '!/[B,b]/{print $0}'
发表于 2024-03-21 13:54:15 回复(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)
sed 's/^.*[b,B].*$//g'
发表于 2023-03-14 16:36:02 回复(0)
sed -r '/[bB]/d' nowcoder.txt 


发表于 2022-10-25 22:50:46 回复(0)
sed '/b\|B/d'
发表于 2022-09-28 17:31:55 回复(0)
grep -Evi b 

发表于 2022-09-19 11:03:44 回复(0)