[SOLVED] bash curly braces and normal parenthesis filter words inclusive space to array
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash curly braces and normal parenthesis filter words inclusive space to array
Hello, try using curly braces and normal parenthesis to remove numbers and first space and read words up to the comma into an array.
Code:
str="a1 word word, a2 word sign, a1b sign word, a2b sign sign"
te2=${str// /.}; arr=(${te2//,./ })
for z in ${arr[@]}; do z=${z#*.}; echo ${z//./ }; done # result
How to separate only from the comma with normal parenthesis?
Is it possible within curly braces common strings continue defining?
Code:
a="a1 word word"
echo ${a#[a-z][0-9]* } # correct
a="a1b2 sign word"
echo ${a#([a-z][0-9]+)* } # wrong=a1b2 sign word / correct=word word
str="a1 word word, a2 word sign, a1b sign word, a2b sign sign"
arr=()
IFS=',' read -ra A <<< "${str[@]}"
for i in "${A[@]}"; do
if [[ "$i" = \ * ]]; then
arr+=$(cut -d " " -f 2-4 <<< "${i#?}")
arr+=" "
else
arr+=$(cut -d " " -f 2-4 <<< "$i")
arr+=" "
fi
done
echo "${arr[@]}"
is an operation on all the (quoted=protected!) array members, without a loop.
The operation here is the # modifier: strip off characters from the left until a not-space followed by a space.
Likewise you can print all the (quoted!) array members:
Code:
orintf "%s\n" "${arr[@]}"
A loop would need another array:
Code:
arr2=()
for a in "${arr[@]}"
do
arr2+=( "${a#*[! ] }" )
done
Three methods to loop over an array:
Code:
# 1. Loop over the member values
for a in "${arr2[@]}"
do
echo "$a"
done
echo
# 2a. Loop over the indexes and get the values
for i in ${!arr2[@]}
do
echo "$[arr2[$i]}"
done
echo
# 2b. Loop over the indexes and get the values
for (( i=0; i < ${#arr2[@]}; i++ ))
do
echo "$[arr2[$i]}"
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.