LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2022, 09:46 AM   #1
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Rep: Reputation: 0
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
 
Old 07-30-2022, 02:12 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Solvable in a number of ways.

Real basic way:
Code:
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[@]}"
 
Old 07-31-2022, 10:02 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Code:
a="a1 word word"
a="a1b2 sign word"
Both words end with a number, and if the following space should be stripped of, too, it becomes
Code:
echo "${a#*[0-9] }"
I don't understand the whole exercise yet. Maybe the followi g makes sense?
Code:
str="a1 word word, a2 word sign, a1b sign word, a2b sign sign"
IFS="," read -ra arr <<< "$str"
arr=( "${arr[@]#*[0-9] }" )
 
Old 07-31-2022, 10:39 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
str="a1 word word, a2 word sign, a1b sign word, a2b sign sign"
IFS="," read -ra arr <<< "$str"
arr=( "${arr[@]#*[0-9] }" )

echo "${arr[@]}"
word word word sign a1b sign word a2b sign sign
This is what I get from that.

I thought that I could tell what the OP wanted from post. Maybe not.
 
Old 07-31-2022, 12:37 PM   #5
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
thank you
 
Old 07-31-2022, 02:18 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Is it solved now? If not, please provide some <input,expected output> pairs.
 
Old 08-01-2022, 12:38 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Still guessing what output is required.

The following variant removes the first word from each array member. (Delete from the left, up to the first non-space and space.)
Code:
str="a1 word word, a2 word sign, a1b sign word, a2b sign sign"
arr=( "${arr[@]#*[! ] }" )
The modifier works on each array member. (Saving a loop.)

Print the array members newline-separated:
Code:
printf "%s\n" "${arr[@]}"
The echo prints its arguments space-separated
Code:
echo "${arr[@]}t"
 
Old 08-01-2022, 09:58 AM   #8
blumenwesen
Member
 
Registered: Jan 2022
Posts: 53

Original Poster
Rep: Reputation: 0
yes solved, are two direct ways.

Code:
IFS="," read -ra arr <<< "a1 word word, a2 word sign, a1b sign word, a2b sign sign"
arr=( "${arr[@]#*[! ] }" ) # or arr=( "${arr[@]#*[a-z0-9] }" )
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
echo ${arr[3]}
 
Old 08-08-2022, 02:12 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Note that
Code:
arr=( "${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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash two commands in curly braces? blumenwesen Programming 23 01-24-2022 12:57 AM
LXer: All about {Curly Braces} in Bash LXer Syndicated Linux News 0 02-28-2019 12:30 AM
[SOLVED] Undefined variable inside Curly braces in the function srinietrx Programming 4 10-13-2015 02:38 AM
LXer: Some inclusive discussion about inclusive decisions LXer Syndicated Linux News 0 10-10-2015 02:39 AM
Unexpected curly braces in expect script spawn command & bash suid problem slinx Programming 1 05-02-2008 01:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration