简体   繁体   English

如何在单行上打印bash数组的输出

[英]How to print output of bash array on single line

My code is 我的代码是

arr=($(awk '/"*"/' output.txt)) 

And output.txt says has following content - 并且output.txt说有以下内容-

user  
{  
'xx123':  
comment
=>  
"John  
Smith",  
groups  
=>  
$groups,  
id  
=>  
'1234',  

This is to search all usernames in my target file.It searches everything enclosed with quotes and stores in array arr. 这是在目标文件中搜索所有用户名,并搜索所有用引号引起来的内容并将其存储在数组arr中。

Current Output: 电流输出:

"John  
Smith",  
"Abc  
Xyz",  
"...  
...",

But i want to achieve this: 但我想实现这一目标:

"John Smith"  
"Abc Xyz"

Currently using a for loop to print this and two other arrays. 当前使用for循环来打印此数组和其他两个数组。

How can I achieve this? 我该如何实现?

I'm making assumptions about the data from the small sample given, but we can start here and you can let me know if some of those assumptions weren't so good. 我正在根据给定的小样本中的数据进行假设,但是我们可以从这里开始,您可以让我知道其中一些假设不是很好。 The main idea is just to not print a new line after reading a line with an opening quote: 主要思想是在阅读带有引号的行后不打印新行:

$  awk '/^"/ { printf "%s ", $0; next } sub(/",$/, "\"")' output.txt
"John Smith"
"Abc Xyz"

$ cat output.txt
user
{
'xx123':
comment
=>
"John
Smith",
groups
=>
$groups,
id
=>
'1234',
=>
"Abc
Xyz",
groups
}

While I agree the awk solution is probably the shortest way to handle the case, the same can be done using the bash built-in comparison, string-indexes and parameter expansion (substring removal). 尽管我同意awk解决方案可能是处理该情况的最短方法,但是使用bash内置的比较,字符串索引和参数扩展(删除子字符串)也可以做到这一点。 For example: 例如:

#!/bin/bash

first=""
while read -r last; do 
    [ "${last:0:1}" = '"' ] && first="$last"
    [ "${last:(-2)}" = '",' ] && printf "%s %s\n" "$first" "${last%,}"
done <"$1"

Will provide the same output for your given input file, eg: 将为您给定的输入文件提供相同的输出,例如:

$ bash merge2l.sh file.txt
"John Smith"
"Abc Xyz"

In my first try I missed your point. 在我的第一次尝试中,我错过了您的观点。 Here is my correction. 这是我的更正。

You can try this: 您可以尝试以下方法:

printf "%s %s\n" "${arr[@]}"

Example: 例:

$ set|grep BASH_VERSINFO
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
$ printf "%s %s\n" "${BASH_VERSINFO[@]}"
3 2
25 1
release i686-redhat-linux-gnu

I hope this is what your are looking for... 我希望这是您正在寻找的...

If there is no space in the array elements, then ${BASH_VERSINFO[@]} or ${BASH_VERSINFO[*]} can be used, but "${BASH_VERSINFO[@]}" is safer. 如果数组元素中没有空格,则可以使用${BASH_VERSINFO[@]}${BASH_VERSINFO[*]} ,但是"${BASH_VERSINFO[@]}"更安全。

On the other hand it is better to implement inside as jas mentioned. 另一方面,最好在jas提到的在内部实现。 If you do some other manipulation with arr then this can be a proper solution. 如果使用arr进行其他操作,那么这可能是一个合适的解决方案。

If the input file is small it can be implemented inside . 如果输入文件很小,则可以在实现。

I think the used pattern "*" is not what you are intended to use. 我认为使用的模式"*"不是您要使用的模式。 This check for zero or more occurrences of " followed by " . 此检查是否出现零个或多个"后跟" I think you wanted to check for ".*" or "[^"]*" or something like these... 我认为您想检查".*""[^"]*"或类似的内容...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM