如何在使用sqlloader时加载一个列,如果CSV列除了标题之外是空的?

huangapple go评论50阅读模式
英文:

How do I load a column using sqlloader if the CSV column is empty except for the header?

问题

以下是您要的翻译部分:

I would like to load a CSV file using sqlloader without having to edit the CSV file each time. When the file is sent to me, the data in the first column is only included in a header above the rest of that data. First code snippet is an example of what I'm receiving with fake data, second code snippet is what I would like to change it to.

我想使用sqlloader加载CSV文件,而不必每次都编辑CSV文件。当文件发送给我时,第一列中的数据只包含在其余数据的标题之上。第一个代码片段是我收到的带有虚假数据的示例,第二个代码片段是我想要更改它的样子。

Each row in the SQL table needs to include the number from row one.

SQL表中的每一行都需要包含来自第一行的数字。

1,Intern,
1,1/8/2023,Bob
1,5/3/2022,Alice
1,7/25/2022,Charles
2,Assistant,
2,1/8/2023,Heather
2,5/3/2022,Harold
2,7/25/2022,Dave
3,Manager,
3,1/1/2023,Tim
3,1/8/2023,Lyon
3,5/3/2022,Greg
3,7/25/2022,Tyler
5,Head Manager,
5,1/8/2023,Charles
5,5/3/2022,Zack

每个SQL表中的行都需要包含来自第一行的数字。

I'm thinking there may be some way to edit them in the shell script that calls the sqlldr command, such as sed or awk.

我认为可以在调用sqlldr命令的shell脚本中使用sed或awk等方法来编辑它们。

英文:

I would like to load a CSV file using sqlloader without having to edit the CSV file each time. When the file is sent to me, the data in the first column is only included in a header above the rest of that data. First code snippet is an example of what I'm receiving with fake data, second code snippet is what I would like to change it to.

Each row in the SQL table needs to include the number from row one.

1,Intern,
,1/8/2023,Bob
,5/3/2022,Alice
,7/25/2022,Charles
2,Assistant,
,1/8/2023,Heather
,5/3/2022,Harold
,7/25/2022,Dave
3,Manager,
,1/1/2023,Tim
,1/8/2023,Lyon
,5/3/2022,Greg
,7/25/2022,Tyler
5,Head Manager,
,1/8/2023,Charles
,5/3/2022,Zack

How I need it to look:

1,Intern,
1,1/8/2023,Bob
1,5/3/2022,Alice
1,7/25/2022,Charles
2,Assistant,
2,1/8/2023,Heather
2,5/3/2022,Harold
2,7/25/2022,Dave
3,Manager,
3,1/1/2023,Tim
3,1/8/2023,Lyon
3,5/3/2022,Greg
3,7/25/2022,Tyler
5,Head Manager,
5,1/8/2023,Charles
5,5/3/2022,Zack

I'm thinking there may be some way to edit them in the shell script that calls the sqlldr command, such as sed or awk.

答案1

得分: 1

使用如问题所示的输入文件(input.csv),以下命令:

输出如下:

1,Intern,
1,1/8/2023,Bob
1,5/3/2022,Alice
1,7/25/2022,Charles
2,Assistant,
2,1/8/2023,Heather
2,5/3/2022,Harold
2,7/25/2022,Dave
3,Manager,
3,1/1/2023,Tim
3,1/8/2023,Lyon
3,5/3/2022,Greg
3,7/25/2022,Tyler
5,Head Manager,
5,1/8/2023,Charles
5,5/3/2022,Zack

解释:

  • -F, 将输入字段分隔符设置为逗号(,)。
  • -v 'OFS=,' 将输出字段分隔符设置为逗号(,)。
  • if($1!="") x=$1 保存第一列中非空值。
  • else $1=x 用保存的值替换空值。
  • 1 始终为真的条件,具有默认动作 print(是 {print} 的缩写)。
英文:

With an input file (input.csv) as shown in the question, the command

awk -F, -v 'OFS=,' '{ if($1!="") x=$1; else $1=x } 1' input.csv

prints

1,Intern,
1,1/8/2023,Bob
1,5/3/2022,Alice
1,7/25/2022,Charles
2,Assistant,
2,1/8/2023,Heather
2,5/3/2022,Harold
2,7/25/2022,Dave
3,Manager,
3,1/1/2023,Tim
3,1/8/2023,Lyon
3,5/3/2022,Greg
3,7/25/2022,Tyler
5,Head Manager,
5,1/8/2023,Charles
5,5/3/2022,Zack

Explanation:

  • -F, set input field separator to ,
  • -v 'OFS=,' set output field separator ,
  • if($1!="") x=$1 save non-empty value from column 1
  • else $1=x replace empty value with saved value
  • 1 always-true condition with default action print (shortcut for {print})

huangapple
  • 本文由 发表于 2023年2月14日 04:49:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定