bash 将 csv 转换为文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3041688/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:12:36  来源:igfitidea点击:

converting a csv into text

bash

提问by user349418

I have a csv (large) file of ip addresses, and wish to covert into single line ip address in bash.

我有一个 csv(大)ip 地址文件,并希望在 bash 中转换为单行 ip 地址。

aa.bb.cc.dd,aa.bb.cc.dd,aa.bb.cc.dd,..

aa.bb.cc.dd,aa.bb.cc.dd,aa.bb.cc.dd,..

into
aa.bb.cc.dd
aa.bb.cc.dd
aa.bb.cc.dd
[..]

进入
aa.bb.cc.dd
aa.bb.cc.dd
aa.bb.cc.dd
[..]

The list of ips in question,

有问题的ip列表,

http://www.stopforumspam.com/downloads/bannedips.zip

http://www.stopforumspam.com/downloads/bannedips.zip

回答by luke

cat file | tr  ',' '\n' > fixed.txt

trdoes simple character translation (and much more but thats what its doing here). this just translates all the commas to newlines.

tr做简单的字符翻译(还有更多,但这就是它在这里所做的)。这只是将所有逗号转换为换行符。

回答by Paused until further notice.

tr ',' '\n' < inputfile > outputfile

For left-to-right dog people:

对于从左到右狗的人:

< inputfile tr ',' '\n' > outputfile

回答by marco

In bash you can use a while loop:

在 bash 中,您可以使用 while 循环:

while read -d, ip; 
    do echo $ip; 
done <file.csv >output

In awk, you can get the same result with less time:

在 awk 中,您可以用更少的时间获得相同的结果:

awk -v RS=, '' file.csv >output

回答by Peter Ajtai

Assuming that file is not on your server, this will do all the work for you in one line:

假设该文件不在您的服务器上,这将在一行中为您完成所有工作:

curl http://www.stopforumspam.com/downloads/bannedips.zip | gunzip -c | sed s/,/\n/g  > bannedips.txt

You can't use unzip for this, if you want it flying through the pipes.

如果你想让它飞过管道,你不能为此使用解压缩

Thanks for the suggestion Dennis!

感谢丹尼斯的建议!