Home > front end >  How can I concatenate text in one row?
How can I concatenate text in one row?

Time:03-23

I'am using MariaDB and I can do this:

`SELECT file_target FROM oc_share WHERE uid_initiator = 'aaa329';

 -------------------------------- 
| file_target                    |
 -------------------------------- 
| /2012                          |
| /ownCloud Manual 1 2 3 4 5.pdf |
 -------------------------------- 

or this:

SELECT CONCAT(file_target) FROM oc_share WHERE uid_initiator = 'aaa329';

 -------------------------------- 
| CONCAT(file_target)            |
 -------------------------------- 
| /2012                          |
| /ownCloud Manual 1 2 3 4 5.pdf |
 -------------------------------- 

so far so easy..., but if I then want to process the result further, the row with the text '/ownCloud Manual 1 2 3 4 5.pdf' is not seen as a continuous string because there are spaces here.

Now my question: How can I get the text into a single string? With a SQL-statement or with bash script?

This is an excerpt from a test bash script:

local Shares=$(mysql -sN -P "${DBPORT}" --host="${DBHOST}" --user="${DBUSER}" --password="${DBPASS}" -e "${SharesCMD}" "${DBNAME}")         
local sharesArray=(${Shares})         
for index in "${!sharesArray[@]}"
do
   echo "$index ${sharesArray[index]}"
done

My output:

0 /2012 
1 /ownCloud 
2 Manual 
3 1 
4 2 
5 3 
6 4 
7 5.pdf

I would like to have:

0 /2012 
1 /ownCloud Manual 1 2 3 4 5.pdf

CodePudding user response:

When you do

local sharesArray=(${Shares})

it splits the variable at whitespace by default, so all the words in /ownCloud Manual 1 2 3 4 5.pdf become separate array elements.

You can use the IFS variable to control how this is split.

IFS=$'\n'
local sharesArray=(${Shares})
  • Related