string 将多行字符串转换为单个逗号分隔的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8714355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turning multi-line string into single comma-separated
提问by Alex Coplan
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
in bash?
在 bash 中?
采纳答案by Dan Fego
You can use awk
and sed
:
您可以使用awk
和sed
:
awk -vORS=, '{ print }' file.txt | sed 's/,$/\n/'
Or if you want to use a pipe:
或者,如果您想使用管道:
echo "data" | awk -vORS=, '{ print }' | sed 's/,$/\n/'
To break it down:
分解:
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/,$//
)
awk
非常擅长处理分解为字段的数据-vORS=,
将“输出记录分隔符”设置为,
,这就是您想要的{ print $2 }
告诉awk
打印每个记录(行)的第二个字段file.txt
是你的文件名sed
只是摆脱尾随,
并将其变成换行符(如果你不想换行,你可以这样做s/,$//
)
回答by Mattias Ahnberg
Clean and simple:
干净简单:
awk '{print }' file.txt | paste -s -d, -
回答by Bhargav Srinivasan
cat data.txt | xargs | sed -e 's/ /, /g'
回答by kev
$ awk -v ORS=, '{print }' data.txt | sed 's/,$//'
+12.0,+15.5,+9.0,+13.5
$ cat data.txt | tr -s ' ' | cut -d ' ' -f 2 | tr '\n' ',' | sed 's/,$//'
+12.0,+15.5,+9.0,+13.5
回答by Rahul Verma
awkone liner
awk一个班轮
$ awk '{printf (NR>1?",":"") }' file
+12.0,+15.5,+9.0,+13.5
回答by jaypal singh
This should work too
这也应该有效
awk '{print }' file | sed ':a;{N;s/\n/,/};ba'
回答by potong
This might work for you:
这可能对你有用:
cut -d' ' -f5 file | paste -d',' -s
+12.0,+15.5,+9.0,+13.5
or
或者
sed '/^.*\(+[^ ]*\).*/{s///;H};${x;s/\n/,/g;s/.//p};d' file
+12.0,+15.5,+9.0,+13.5
or
或者
sed 's/\S\+\s\+//;s/\s.*//;H;$!d;x;s/.//;s/\n/,/g' file
For each line in the file; chop off the first field and spaces following, chop off the remainder of the line following the second field and append to the hold space. Delete all lines except the last where we swap to the hold space and after deleting the introduced newline at the start, convert all newlines to ,
's.
对于文件中的每一行;砍掉第一个字段和后面的空格,砍掉第二个字段后面的行的其余部分并附加到保持空间。删除除我们交换到保留空间的最后一行之外的所有行,并在删除开头引入的换行符后,将所有换行符转换为,
's。
N.B. Could be written:
NB 可以写成:
sed 's/\S\+\s\+//;s/\s.*//;1h;1!H;$!d;x;s/\n/,/g' file
回答by Vonton
Try this easy code:
试试这个简单的代码:
awk '{printf("%s,",)}' File1
回答by kenorb
You can use grep
:
您可以使用grep
:
grep -o "+\S\+" in.txt | tr '\n' ','
which finds the string starting with +
, followed by any string \S\+
, then convert new line characters into commas. This should be pretty quick for large files.
它找到以 开头的字符串+
,后跟任何字符串\S\+
,然后将换行符转换为逗号。对于大文件,这应该很快。
回答by Aquarius Power
try this:
尝试这个:
sedSelectNumbers='s".* \(+[0-9]*[.][0-9]*\) .*","'
sedClearLastComma='s"\(.*\),$""'
cat file.txt |sed "$sedSelectNumbers" |tr -d "\n" |sed "$sedClearLastComma"
the good thing is the easy part of deleting newline "\n" characters!
好消息是删除换行符“\n”字符的简单部分!
EDIT: another great way to join lines into a single line with sed is this: |sed ':a;N;$!ba;s/\n/ /g'
got from here.
编辑:另一种使用 sed 将行连接成一行的好方法是:|sed ':a;N;$!ba;s/\n/ /g'
got from here。