如何将文本连接成一行?
我正在使用 MariaDB,我可以这样做:
`SELECT file_target FROM oc_share WHERE uid_initiator = 'aaa329';
+--------------------------------+
| file_target |
+--------------------------------+
| /2012 |
| /ownCloud Manual 1 2 3 4 5.pdf |
+--------------------------------+
或这样:
SELECT CONCAT(file_target) FROM oc_share WHERE uid_initiator = 'aaa329';
+--------------------------------+
| CONCAT(file_target) |
+--------------------------------+
| /2012 |
| /ownCloud Manual 1 2 3 4 5.pdf |
+--------------------------------+
到目前为止很简单...,但如果我想进一步处理结果,则带有文本“/ownCloud Manual 1 2 3 4 5.pdf”的行不被视为连续字符串,因为这里有空格。
现在我的问题是:如何将文本转换为单个字符串?使用 SQL 语句还是使用 bash 脚本?
这是测试 bash 脚本的摘录:
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
我的输出:
0 /2012
1 /ownCloud
2 Manual
3 1
4 2
5 3
6 4
7 5.pdf
我想要:
0 /2012
1 /ownCloud Manual 1 2 3 4 5.pdf
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您
这样做时,默认情况下会在空格处拆分变量,因此
/ownCloud Manual 1 2 3 4 5.pdf
中的所有单词都会成为单独的数组元素。您可以使用
IFS
变量来控制分割方式。When you do
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.