I can split the next string
1 - Tiendas HD StoreID,SellerID,Country,Flag,StoreName,Address,City,Latitude,Longitude,postalCode,state,localNumber,referenceLocation,businessUnit,fantasyName,legalName,alternativeId,assigneeDocumentNumber,assigneeDocumentType,assigneeEmail,assigneePhone,externalId,initHour,endHour,active,additionalData N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}" N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}" N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}"
2 - Tiendas RT StoreID,SellerID,Country,Flag,StoreName,Address,City,Latitude,Longitude,postalCode,state,localNumber,referenceLocation,businessUnit,fantasyName,legalName,alternativeId,assigneeDocumentNumber,assigneeDocumentType,assigneeEmail,assigneePhone,externalId,initHour,endHour,active,additionalData N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}"
I want split this string with by "$number -", example
"1 - text a"
Or
"1
2 - text b"
to take the follow result
"text a"
"text b"
I have the next code
s='
1 - Tiendas HD StoreID,SellerID,Country,Flag,StoreName,Address,City,Latitude,Longitude,postalCode,state,localNumber,referenceLocation,businessUnit,fantasyName,legalName,alternativeId,assigneeDocumentNumber,assigneeDocumentType,assigneeEmail,assigneePhone,externalId,initHour,endHour,active,additionalData N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}" N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}" N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}"
2 - Tiendas RT StoreID,SellerID,Country,Flag,StoreName,Address,City,Latitude,Longitude,postalCode,state,localNumber,referenceLocation,businessUnit,fantasyName,legalName,alternativeId,assigneeDocumentNumber,assigneeDocumentType,assigneeEmail,assigneePhone,externalId,initHour,endHour,active,additionalData N001,1,Chile,Nike,Nike Las Condes,"Av. Pdte. Kennedy Lateral 9001, Las Condes, Región Metropolitana",Santiago,35.3333,21.2222,788977,Santiago,53A,frente al Easy,HD,Nike HD Kennedy,Nike LF Store LLC,N205,20833673‑8,RUT,[email protected],569888777,vtexnikeqa001,09:00,20:00,1,"{ propety1: 3272, propetyN: ""default"",}"'
#String
text=$s
input=$text
IFS='([0-9 ]*[-] )|(\s\s [0-9 ]*[-] )' array=($input)
for element in "${array[@]}";
do
echo "---> $element" ;
done
Replit https://replit.com/@JoseCastillo20/BASH#main.sh
CodePudding user response:
To split a string on regex pattern, would you please try the following:
#!/bin/bash
s='
1 - text a
2 - text b
'
pat='[[:space:]]*[0-9] -' # pattern to split on
# loop to split the string
while [[ $s =~ ^(.*)$pat(.*)$ ]]; do
array =( "${BASH_REMATCH[2]%"${BASH_REMATCH[2]##*[![:space:]]}"}" )
s="${BASH_REMATCH[1]}" # update "s" with the remaining substring
done
# print the array elements
for (( i = ${#array[@]} - 1; i >= 0; i-- )); do
echo "---> ${array[$i]}"
done
Output:
---> text a
---> text b
- The
[[ $s =~ ^(.*)$pat(.*)$ ]]condition splits$son the pattern$patassigning${BASH_REMATCH[1]}to the left substring and${BASH_REMATCH[2]}to the right wrt the pattern. Due to the nature oflongest matchof regex, the last element${BASH_REMATCH[2]}is determined first, while${BASH_REMATCH[1]}still may contain other patterns to be matched. - Then
sis updated with${BASH_REMATCH[1]}, the left substring, and thewhileloop continues until no more matches remain. ${BASH_REMATCH[2]%"${BASH_REMATCH[2]##*[![:space:]]}"}removes trailing space characters (whitespces, tabs, newlines, etc.) out of${BASH_REMATCH[2]}.- As the array holds the elements in reverse order, the
forloop prints the elements from the last to the first.
CodePudding user response:
$IFS is not interpreted as a regex, but as a list of characters.
