使用数字序列使用awk将列添加到CSV文件 [英] Adding column to CSV file with awk using number sequence

查看:235
本文介绍了使用数字序列使用awk将列添加到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下awk命令.我的目标是使用数字序列使用该序列的值在文件中创建一个新列.最后,我想用每个新列生成CSV文件:

I am trying to use the following awk command. My goal is to use the number sequence to create a new column in my files using the values of the sequence. In the end, I want to generate CSV files with each of the new columns:

for i in $(seq 0 0.5 70); do 
awk -F"," 'BEGIN {OFS = ","} {$7=$(echo $i); print}' ClassificationTest.csv > new_columns_$i.csv;
done

输入的CSV文件如下所示:

The input CSV file looks like this:

Sample1,Probe1,Temp1,ProbeType1,SampleType1,Ratio1
Sample2,Probe2,Temp2,ProbeType2,SampleType2,Ratio2

预期结果应如下所示:

名为new_columns_20.0.csv的文件

A file named new_columns_20.0.csv

Sample1,Probe1,Temp1,ProbeType1,SampleType1,Ratio1,20.0
Sample2,Probe2,Temp2,ProbeType2,SampleType2,Ratio2,20.0

我的目标是为数字序列的每个值创建一个文件.我很难传递序列的值作为参数.有人可以提供有关如何执行此操作的帮助吗?谢谢.

My goal is to have a file for each value of the number sequence. I am having a hard time passing the values of the sequence as arguments. Can someone please provide help on how to perform this? Thank you.

推荐答案

这是一种实现方式

for i in {0..140}; 
do awk -F, -v OFS=, -v i=$i '{$7=i/2}1' input > output_$i; 
done

或完全在awk

awk -F, -v OFS=, '{for(i=0;i<=70;i+=0.5) 
                      {$7=i; print > "output_"i}}' input

这篇关于使用数字序列使用awk将列添加到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆