击翻多行字符串转换成一个逗号分隔 [英] Bash turning multi-line string into single comma-separated
本文介绍了击翻多行字符串转换成一个逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说我有以下字符串:
Let's say I have the following string:
something1: +12.0 (some unnecessary trailing data (this must go))
something2: +15.5 (some more unnecessary trailing data)
something4: +9.0 (some other unnecessary data)
something1: +13.5 (blah blah blah)
我如何把它转换成简单
How do I turn that into simply
+12.0,+15.5,+9.0,+13.5
在bash?
推荐答案
您可以使用 AWK
和 SED
:
awk -vORS=, '{ print $2 }' file.txt | sed 's/,$/\n/'
或者,如果你想使用管道:
Or if you want to use a pipe:
echo "data" | awk -vORS=, '{ print $2 }' | sed 's/,$/\n/'
要打破它:
-
AWK
在细分领域的处理数据是伟大 -
-vORS =,
设置输出记录分隔符来,
,这是你想要的 -
{打印$ 2}
告诉AWK
来打印第二场对每个记录(行) -
file.txt的
是文件名 -
SED
刚刚摆脱尾随的,
键,把它变成一个换行符(如果你想不换行,你可以做S /,$ //
)
awk
is great at handling data broken down into fields-vORS=,
sets the "output record separator" to,
, which is what you wanted{ print $2 }
tellsawk
to print the second field for every record (line)file.txt
is your filenamesed
just gets rid of the trailing,
and turns it into a newline (if you want no newline, you can dos/,$//
)
这篇关于击翻多行字符串转换成一个逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文