Removing single-character words

I am looking for a regular expression to remove a single character word. I do not mind using perl, awk, sedor bashbuilt-in modules.

Test case:

$ echo "a b c d e f g h ijkl m n opqrst u v" | $COMMAND

Required Conclusion:

ijkl opqrst

What I have tried so far:

$ echo "a b c d e f g h ijkl m n opqrst u v" | sed 's/ . //g'
acegijkln opqrstv

I suppose that:

  • a not deleted because there is no space before it
  • cit remains that after removal bthere are no more spaces in front of it
  • etc.

Attempt number 2:

$ echo "a b c d e f g h ijkl m n opqrst u v" | sed 's/\w.\w//g'
     s v

Here I do not understand what is happening.

Any help + explanation is appreciated, I want to find out.

+4
source share
8 answers

\b () \< \> , .

echo "a b c d e f g h ijkl m n opqrst u v" | sed 's/\b\w\b \?//g'

()

echo "a b c d e f g h ijkl m n opqrst u v" | sed 's/\<.\> \?//g'
+7

grep:

echo "a b c d e f g h ijkl m n opqrst u v"  | grep -o '[a-z]\{2,\}'

, .

-o grep ( ).

+4

, Awk , , , length(). POSIX , .

echo "a b c d e f g h ijkl m n opqrst u v" | \
  awk '{for(i=1;i<=NF;i++) {if (length($i)>1) { printf "%s ", $i }} }'
ijkl opqrst
+2

Perl: length

echo "a b c d e f g h ijkl m n opqrst u v" | perl -lanE \
  'say join " ", grep {length($_) > 1} @F'
+1

, :

$ echo "a b c d e f g h ijkl m n opqrst u v" | tr ' ' '\n' | grep .. | paste -sd " "
ijkl opqrst
+1

linux sprung, , , (a)

(?:\s\w\b|\b\w\s)

$ echo "a b c d e f g h ijkl m n opqrst u v" | sed 's/(?:\s\w\b|\b\w\s)//g'

, , .

regex101.

0

awk. ([^ ]) . .

$ awk '{while(sub(/^[^ ] | [^ ]$/,"")||sub(/ [^ ] /," "));}1'

Using sub, it replaces the tuples with a [a space][non-space][a space]space and deletes single characters from the beginning and end of the record and the leading / trailing space. He is in while, so he continues to do so until there are no punches left. To check this:

$ echo "a b c d e f g h ijkl m n opqrst u v"|awk '{while(sub(/^[^ ] | [^ ]$/,"")||sub(/ [^ ] /," "));}1'
ijkl opqrst
0
source
echo "a b c d e f g h ijkl m n opqrst u v"  | grep -wo "\b[a-z][a-z]\+\b"
-1
source

Source: https://habr.com/ru/post/1666990/


All Articles