开发者

Delete the word whose length is less than 2 in bash

I am using bash on CentOS 5.5. I have a string separated by space , and the st开发者_如何转开发ring 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"


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


sed does this nicely:

example="This is a lovey 7 words string"
echo $example | sed -e 's/ [a-zA-Z0-9]\{1\} / /g'


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'


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜