如何将文本连接成一行?

发布于 2025-01-16 13:15:51 字数 1280 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2025-01-23 13:15:51

当您

local sharesArray=(${Shares})

这样做时,默认情况下会在空格处拆分变量,因此 /ownCloud Manual 1 2 3 4 5.pdf 中的所有单词都会成为单独的数组元素。

您可以使用 IFS 变量来控制分割方式。

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

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})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文