【问题标题】:Copy one csv header to another csv with type modification通过类型修改将一个 csv 标头复制到另一个 csv
【发布时间】:2021-03-17 23:05:55
【问题描述】:

我想通过一些修改将一个 csv 标题逐行复制到另一个

输入 csv

name,"Mobile Number","mobile1,mobile2",email2,Address,email21
test, 123456789,+123456767676,a@test.com,testaddr,a1@test.com
test1,7867778,8799787899898,b@test,com, test2addr,b2@test.com

在新的 csv 中,这应该是这样的,并且还应该创建文件。对于 sting 列,我将传递列名,因此只有该列将转换为字符串

name.auto()
Mobile Number.auto()
mobile1,mobile2.string()
email2.auto()
Address.auto()
email21.auto()

正如您在上面看到的,所有这些带有类型修改的标题应该插入不同的行中

我尝试过使用以下命令,但这仅适用于复制第一行

sed '1!d' input.csv > output.csv

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 欢迎来到 Stack Overflow。 SO is a question and answer page for professional and enthusiast programmers。请在您的问题中添加您自己的代码。您应该至少展示自己为解决这个问题所做的研究。
  • @Tushar 我已经努力更新了这个问题。
  • @user13000875 - 我无法理解输出要求。是否要将标题行拆分为多行?
  • 使用.string()的标准是什么?

标签: bash shell awk sed


【解决方案1】:

您也可以试试这个替代的gnu awk 命令:

awk -v FPAT='"[^"]+"|[^,]+' 'NR == 1 {
   for (i=1; i<=NF; ++i)
      print gensub(/"/, "", "g", $i) "." ($i ~ /,/ ? "string" : "auto") "()"
   exit
}' file
name.auto()
Mobile Number.auto()
mobile1,mobile2.string()
email2.auto()
Address.auto()
email21.auto()

或者使用sed:

sed -i -e '1i 1234567890.string(),My address is test.auto(),abc3@gmail.com.auto(),120000003.auto(),abc-003.auto(),3.com.auto()' -e '1d' test.csv

【讨论】:

  • 您的解决方案打印完美,但是当我将此输出保存到文件时,mobile1 保存在一个列中,而 mobile2.string() 位于另一列
  • 您的输入文件中有一些 DOS 行尾吗?你能显示head file | cat -A 命令的输出吗?
  • name,"手机号码","mobile1,mobile2",email2,Address,email21$。运行头文件后| cat -A 命令
  • 好的,然后运行awk -v FPAT='"[^"]+"|[^,]+' 'NR==1 {for (i=1; i&lt;=NF; ++i) print gensub(/"/, "", "g", $i) "." ($i ~ /,/ ? "string()" : "auto()"); exit}' file &gt; out; cat -A out
【解决方案2】:

编辑:根据 OP 的评论仅打印第一行(标题),请尝试以下操作。

awk -v FPAT='[^,]*|"[^"]+"' '
FNR==1{
  for(i=1;i<=NF;i++){
    if($i~/^".*,.*"$/){
      gsub(/"/,"",$i)
      print $i".string()"
    }
    else{
      print $i".auto()"
    }
  }
  exit
}
' Input_file > output_file


您能否尝试使用 GUN awk 进行跟踪、编写和测试,并附上示例。

awk -v FPAT='[^,]*|"[^"]+"' '
FNR==1{
  for(i=1;i<=NF;i++){
    if($i~/^".*,.*"$/){
      gsub(/"/,"",$i)
      print $i".string()"
    }
    else{
      print $i".auto()"
    }
  }
  next
}
1
' Input_file

说明:为上述添加详细说明。

awk -v FPAT='[^,]*|"[^"]+"' '  ##Starting awk program and setting FPAT to [^,]*|"[^"]+".
FNR==1{                        ##Checking condition if this is first line then do following.
  for(i=1;i<=NF;i++){          ##Running for loop from i=1 to till NF value.
    if($i~/^".*,.*"$/){        ##Checking condition if current field starts from " and ends with " and having comma in between its value then do following.
      gsub(/"/,"",$i)          ##Substitute all occurrences of " with NULL in current field.
      print $i".string()"      ##Printing current field and .string() here.
    }
    else{                      ##else do following.
      print $i".auto()"        ##Printing current field dot auto() string here.
    }
  }
  next                         ##next will skip all further statements from here.
}
1                              ##1 will print current line.
' Input_file                   ##Mentioning Input_file name here.

【讨论】:

  • 我只想要这个作为标题。
  • @user13000875,好的,现在编辑答案,它将编辑第一行并按原样打印其余行,让我知道。
  • 我不想打印到新文件中的其余行。只有标题需要打印
  • @user13000875,请尝试我的 EDIT 解决方案一次,然后告诉我?
  • @your 解决方案在标题列中没有空格和逗号时有效。但是当标题列中的空格或逗号时,所有数据都打印在单行中
猜你喜欢
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多