bash 将 unix 输出发送到 csv 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27388136/
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
send the unix output to a csv file
提问by Django
I want to put the output data from unix command to a csv file. Suppose the output which I am getting is :
我想将 unix 命令的输出数据放到一个 csv 文件中。假设我得到的输出是:
A
B
C
I want to put this data in .csv file as
我想将此数据放入 .csv 文件中
A B C
in three different columns but same row.
在三个不同的列但同一行。
回答by Gilles Quenot
Try this :
尝试这个 :
printf '%s\n' A B C | paste -sd ' ' >> file.csv
or more classical for a CSV (delimiter with a ,
:
或更经典的 CSV(带有 的分隔符,
:
printf '%s\n' A B C | paste -sd ',' >> file.csv
printf '%s\n' A B C
is just an example to have the same sample input as you. My solution works with spaces in a same line too.
printf '%s\n' A B C
只是与您具有相同样本输入的示例。我的解决方案也适用于同一行中的空格。
EDITfrom your comments, you seems to need to treat with a for
loop, so :
从您的评论中编辑,您似乎需要使用for
循环进行处理,因此:
for i in {0..5}; do printf '%s\n' {A..C} | paste -sd " " >> file.csv; done
or in pseudo code :
或在伪代码中:
for ...:
unix_command | paste -sd " " >> file.csv
endfor
回答by Cyrus
unix_command | tr "\n" " " > file.csv
or
或者
unix_command | awk 'ORS=FS' > file.csv
回答by BMW
For my understanding, @Django needs three line into one line.
据我了解,@Django 需要三行合二为一。
paste -d ' ' - - - < infile
If you need output as csv format (split by ,
), you can use this
如果您需要输出为 csv 格式(拆分为,
),您可以使用此
paste -d ',' - - - < infile
Here is the test result
这是测试结果
$ cat infile
Manoj Mishra
Japan
Environment.
Michael Hymanson
America
Environment.
$ paste -d ',' - - - < infile
Manoj Mishra,Japan,Environment.
Michael Hymanson,America,Environment.
回答by gboffi
A more general answer
更一般的答案
If the output of your command is multi-line and you want to put the
quotedoutput in csv format, n
items per line, the following script
could be handy.
如果您的命令的输出是多行的,并且您希望将
引用的输出以 csv 格式(n
每行项目)放置,则以下脚本可能会很方便。
The groupby
program reads from stdin
and
该groupby
程序从stdin
和
- quotes each input line
- groups
n
quoted input lines in a csv record, using a comma as a separator
- 引用每个输入行
n
在 csv 记录中对引用的输入行进行分组,使用逗号作为分隔符
optionally, using the -s
optional argument, the program discards the
last line of its output if said last line doesn't contain exactly n
items.
可选地,使用-s
可选参数,如果所述最后一行不完全包含n
项目,程序将丢弃其输出的最后一行。
The -h
option, as usual, echoes an usage line and exits.
-h
像往常一样,该选项会回显一条用法行并退出。
Specifying another option the program prints the usage line and exits in error.
指定另一个选项,程序会打印用法行并错误退出。
The code
编码
% cat groupby
#!/bin/sh
usage () { echo Usage: % chmod +x groupby
% echo -e "1\n2\n3\n4\n5" | ./groupby 3
"1","2","3"
"4","5"
% echo -e "1\n2\n3\n4\n5\n6" | ./groupby 3
"1","2","3"
"4","5","6"
echo -e "1\n2\n3\n4\n5\n6\n7" | ./groupby 3
"1","2","3"
"4","5","6"
"7"
% echo -e "1\n2\n3\n4\n5\n6\n7\n8" | ./groupby -s 4
"1","2","3","4"
"5","6","7","8"
% echo -e "1\n2\n3\n4\n5\n6\n7" | ./groupby -s 4
"1","2","3","4"
%
[-s] n --- -s is for \"strict\", outputs only records of n items. ; exit ; }
s=0
while getopts :sh o ; do
case "${o}" in
s) s=1 ; shift ;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
awk -v n= -v s=$s -v q='"' '
NR==1 {buf = q #!/bin/sh
usage () { echo 'Usage: 'less filename| xargs >> filename.csv
' [-s] [-q quote_char] [-c separator_char] n
Reads lines from stdin and prints them grouped by n and separated by spaces.
Optional arguments:
-s is for "strict", outputs only records of n items;
-q quote_char, forces quoting of each input line;
-c separator_char, changes the field separator,
interesting alternatives are tab, comma, semicolon etc;
-h prints this help and exits.' ; exit ; }
# Default options
s=0 ; q='' ; c=' '
# Treatment of optional arguments
while getopts :shc:q: o ; do
case "${o}" in
s) s=1 ; ;;
c) c="${OPTARG}" ;;
q) q="${OPTARG}" ;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
shift $(($OPTIND-1))
# awk code
awk -v n= -v s=$s -v q="$q" -v c="$c" '
NR==1 {buf = q ##代码## q ; next}
NR%n==1 {print buf; buf = q ##代码## q ; next}
{buf = buf c q ##代码## q}
END {if(!s||NR%n==0)print buf}'
q ; next}
NR%n==1 {print buf; buf = q ##代码## q ; next}
{buf = buf "," q ##代码## q}
END {if(!s||NR%n==0)print buf}'
%
An example of usage
使用示例
##代码##A different angle
不一样的角度
I changed the defaults to suit best the OP requirements, and introduced other options, see the usage string for details
我更改了默认值以最适合 OP 要求,并引入了其他选项,请参阅用法字符串了解详细信息
##代码##