Delete the word whose length is less than 2 in bas

2019-02-25 15:28发布

I am using bash on CentOS 5.5. I have a string separated by space , and the string only contain alphabets and numbers, and this string may have extra space, for example, there are more than 1 space between "words" and "string":

$exmple= "This is a lovey 7 words   string"

I want to delete the word whose length is less than 2, in this example, the word "a" and "7" need to be deleted. And delete all the extra space, only one space between one word and another.

so the string becomes:

"This is lovey words string"

4条回答
We Are One
2楼-- · 2019-02-25 16:15

Edit (based on ennuikiller's sed answer)

Using pure Bash:

newstring=${exmple// ? / }   # remove one character words

To normalize the whitespace:

read newstring <<< $newstring

or

shopt -s extglob
newstring=${newstring//+( )/ }

Original:

exmple="This is a lovey 7 words   string"
for word in $exmple
do
    if (( ${#word} >= 2 ))
    then
        newstring+=$sp$word
        sp=' '
    fi
done
查看更多
欢心
3楼-- · 2019-02-25 16:28

sed -e 's/ [a-zA-Z0-9] / /g' won't remove double or more spaces.

This will:

echo "This is a lovey 7 words   string" | sed 's/ [a-zA-Z0-9 ] / /g'

And this will remove any spaces from the beginning or from the end:

echo "   This is a lovey 7 words   string  " | sed 's/ [a-zA-Z0-9 ] / /g' | sed 's/^ *\| *$//g'
查看更多
甜甜的少女心
4楼-- · 2019-02-25 16:29

sed does this nicely:

example="This is a lovey 7 words string"
echo $example | sed -e 's/ [a-zA-Z0-9]\{1\} / /g'
查看更多
smile是对你的礼貌
5楼-- · 2019-02-25 16:32

awk can also make it:

$ awk '{for (i=1; i<=NF; i++) s=(length($i)>2? s($i)FS : s); print s}' <<< "This is a lovey 7 words   string"
This lovey words string 

Explanation

The idea is to loop through all the fields of the string storing those being bigger than a given size. Finally, print the stored string.

  • for (i=1; i<=NF; i++) loop through all fields.
  • s=(length($i)>2? s($i)FS : s) if the length of the word is bigger than 2, then append it to the current sentence. Otherwise, not.
  • print s print the final string.
查看更多
登录 后发表回答