Marvive
Marvive

Reputation: 23

Delete words with capital letters in bash

How do you delete words in a string in BASH that have capital letters? Ex.

Input:

Taco burrito Mexico fiesta

Output:

burrito fiesta

The solution I saw on Stack Overflow doesn't work for me since the linux machine I'm working with doesn't accept the -r switch.

Upvotes: 2

Views: 501

Answers (4)

agc
agc

Reputation: 8406

Pure bash:

set -- Taco burrito Mexico fiesta ; LC_ALL=C; echo ${@/*[A-Z]*}

Or:

a=(Taco burrito Mexico fiesta) ; LC_ALL=C; echo ${a[@]/*[A-Z]*}

Output (of either):

burrito fiesta

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246877

For fun, perl

perl -aE 'say "@{[ grep {not /^[[:upper:]]/} @F ]}"' file

Upvotes: 1

steffen
steffen

Reputation: 16968

$ echo Taco burrito Mexico fiesta | awk '{gsub(/ (\S*[A-Z]\S*)|(\S*[A-Z]\S* )/, "")}1'
burrito fiesta

Upvotes: 0

anubhava
anubhava

Reputation: 785286

You may use this sed:

s='Taco burrito Mexico fiesta'
sed 's/[[:upper:]][^[:blank:]]*[[:blank:]]*//g' <<< "$s"

burrito fiesta

Details:

  • [[:upper:]]: Matches an uppercase letter
  • [^[:blank:]]: Matches any character except a space or tab character
  • [[:blank:]]: Matches a space or tab character

Upvotes: 1

Related Questions