简体   繁体   English

在CSV文件上添加新行

[英]Add new lines on CSV file

I want to transform a CSV file using bash by adding new lines into it depending on some conditions described below : 我想通过bash转换CSV文件,具体方法如下所述:

CSV file structure: CSV文件结构:

name,id_name,url
Amy,N1,http://google.com
Rob,N2,http://google.com http://other-url.com http://anotherurl.com http://other-again.com
Johh,N3,http://google.com http://anotherurl.com
Jack,N4,http://google.com http://other-url.com
...

I want to transform the CSV file like this : 我想像这样转换CSV文件:

name,id_name,url
Amy,N1,http://google.com
Rob,N2,http://google.com
Rob,N2,http://other-url.com
Rob,N2,http://anotherurl.com 
Johh,N3,http://google.com
Johh,N3,http://anotherurl.com
Jack,N4,http://google.com 
Jack,N4,http://other-url.com
...

Thanks 谢谢

It is just a matter of splitting the last field and then printing the 1st and 2nd field followed by the set of these slices: 只需拆分最后一个字段,然后打印第一个和第二个字段,然后打印这些切片集即可:

awk 'BEGIN{FS=OFS=","}{n=split($NF,a," "); for (i=1;i<=n;i++) print $1,$2,a[i]}' file

Which returns: 哪个返回:

Amy,N1,http://google.com
Rob,N2,http://google.com
Rob,N2,http://other-url.com
Rob,N2,http://anotherurl.com
Rob,N2,http://other-again.com
Johh,N3,http://google.com
Johh,N3,http://anotherurl.com
Jack,N4,http://google.com
Jack,N4,http://other-url.com

This awk should work: 这个awk应该工作:

awk -F '[, ]' -v OFS=, '{for (i=3; i<=NF; i++) print $1, $2, $i}' file
name,id_name,url
Amy,N1,http://google.com
Rob,N2,http://google.com
Rob,N2,http://other-url.com
Rob,N2,http://anotherurl.com
Rob,N2,http://other-again.com
Johh,N3,http://google.com
Johh,N3,http://anotherurl.com
Jack,N4,http://google.com
Jack,N4,http://other-url.com
  • -F '[, ]' sets field separator as comma or space. -F '[, ]'将字段分隔符设置为逗号或空格。
  • Then just start iterating from field #3 and print it along with first 2 fields. 然后只需从字段#3开始迭代,然后将其与前两个字段一起打印即可。

with bash 重击

while IFS=, read name id url; do
  set -f
  for u in $url; do
    echo "$name,$id,$u"
  done
  set +f
done < file
name,id_name,url
Amy,N1,http://google.com
Rob,N2,http://google.com
Rob,N2,http://other-url.com
Rob,N2,http://anotherurl.com
Rob,N2,http://other-again.com
Johh,N3,http://google.com
Johh,N3,http://anotherurl.com
Jack,N4,http://google.com
Jack,N4,http://other-url.com

This will not pring any records for which the url field is empty. 这不会对url字段为空的任何记录进行预加载。

I'm taking advantage of shell word-splitting with the unquoted variable in the for-loop. 我利用了在for循环中使用未引用变量的shell 单词拆分功能 For safety, I'm turning off filename expansion while I do that. 为了安全起见,我将关闭文件扩展名

perl -F'[, ]' -lane 'for ($i=2; $i<=$#F; $i++) {print "$F[0],$F[1],$F[$i]"}' file

-a autosplits each line into the @F array -a将每一行自动拆分为@F数组
-F'[, ]' autosplit field separator is either a comma or a space -F'[, ]'分割字段分隔符可以是逗号或空格
$#F is the index of the last element of the @F array $#F@F数组的最后一个元素的索引
perl arrays start with index 0 , while awk starts with 1 perl数组以索引0开头,而awk以1开头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM