Bash将多行string转换为单逗号分隔

比方说,我有以下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) 

我怎么把它变成简单的

 +12.0,+15.5,+9.0,+13.5 

在bash?

你可以使用awksed

 awk -vORS=, '{ print $2 }' file.txt | sed 's/,$/\n/' 

或者如果你想使用pipe道:

 echo "data" | awk -vORS=, '{ print $2 }' | sed 's/,$/\n/' 

打破它:

  • awkawk处理分解成字段的数据
  • -vORS=,将“输出logging分隔符”设置为,这就是你想要的
  • { print $2 }告诉awk打印每个logging的第二个字段(行)
  • file.txt是你的文件名
  • sed只是摆脱尾随,并将其变成一个换行符(如果你不想换行符,你可以做s/,$//

清洁和简单:

 awk '{print $2}' < file.txt | paste -s -d, - 
 $ awk -v ORS=, '{print $2}' 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 

这也应该工作

 awk '{print $2}' file | sed ':a;{N;s/\n/,/};ba' 

这可能适合你:

 cut -d' ' -f5 file | paste -d',' -s +12.0,+15.5,+9.0,+13.5 

要么

 sed '/^.*\(+[^ ]*\).*/{s//\1/;H};${x;s/\n/,/g;s/.//p};d' file +12.0,+15.5,+9.0,+13.5 

尝试这个:

 sedSelectNumbers='s".* \(+[0-9]*[.][0-9]*\) .*"\1,"' sedClearLastComma='s"\(.*\),$"\1"' cat file.txt |sed "$sedSelectNumbers" |tr -d "\n" |sed "$sedClearLastComma" 

好东西是删除换行符“\ n”字符的简单部分!

编辑:另一个很好的方式来连接线与sed单行是这样的: |sed ':a;N;$!ba;s/\n/ /g'从这里得到。

你可以使用grep

 grep -o "+\S\+" in.txt | tr '\n' ',' 

它会查找以+开头的string,然后是任何string\S\+ ,然后将新的行字符转换为逗号。 对于大文件,这应该很快。

用awk没有看到这个简单的解决scheme

 awk 'b{b=b","}{b=b$2}END{print b}' infile 

用perl:

 fg@erwin ~ $ perl -ne 'push @l, (split(/\s+/))[1]; END { print join(",", @l) . "\n" }' <<EOF 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) EOF +12.0,+15.5,+9.0,+13.5 

你也可以用两个sed调用它:

 $ cat file.txt 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) $ sed 's/^[^:]*: *\([+0-9.]\+\) .*/\1/' file.txt | sed -e :a -e '$!N; s/\n/,/; ta' +12.0,+15.5,+9.0,+13.5 

第一次sed调用删除不感兴趣的数据,第二次join所有行。

你也可以这样打印:

只是awk:使用printf

 bash-3.2$ cat sample.log 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) bash-3.2$ awk ' { if($2 != "") { if(NR==1) { printf $2 } else { printf "," $2 } } }' sample.log +12.0,+15.5,+9.0,+13.5 

另一个Perl解决scheme,类似于Dan Fego的awk:

 perl -ane 'print "$F[1],"' file.txt | sed 's/,$/\n/' 

-a告诉perl把input行分成@F数组,索引从0开始。

用纯Bash编写的解决scheme:

 #!/bin/bash sometext="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)" a=() while read -r a1 a2 a3; do # we can add some code here to check valid values or modify them a+=("${a2}") done <<< "${sometext}" # between parenthesis to modify IFS for the current statement only (IFS=',' ; printf '%s: %s\n' "Result" "${a[*]}") 

结果:+ 12.0,+ 15.5,+ 9.0,+ 13.5

 cat data.txt | xargs | sed -e 's/ /, /g' 

awk一个class轮

 $ awk '{printf (NR>1?",":"") $2}' file +12.0,+15.5,+9.0,+13.5