如何使用新行号打印和存储csv文件中的特定命名列 [英] How to print and store specific named columns from csv file with new row numbers

查看:92
本文介绍了如何使用新行号打印和存储csv文件中的特定命名列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先要说的是,我对使用bash和一般任何类型的脚本编写都是新手.

start by saying, I'm very new to using bash and any sort of script writing in general.

我有一个csv文件,该文件具有基本的列标题和下面的值,例如:

I have a csv file that has basic column headers and values underneath which looks something like this as an example:

a  b  c  d
3  3  34 4
2  5  4  94
4  5  8  3
9  8  5  7

是否有一种方法只能从特定列中提取数值,并为每行添加一个数字.例如,第一列的第一行(从列标题后的1开始)为1,然后为2,然后为3,依此类推,例如对于b列,输出为:

Is there a way to extract only the numerical values from a specific column and add a number for each row. For example first numbered row of the first column (starting from 1 after the column header) is 1, then 2, then 3, etc, for example for column b the output would be:

1  3
2  5
3  5
4  8

我希望能够对各种不同的命名列标题执行此操作.

I would like to be able to do this for various different named column headers.

任何帮助,我们都会感激的,

Any help would be appreciated,

克里斯

推荐答案

喜欢吗?使用awk:

$ awk 'NR>1{print NR-1, $2}' file
1 3
2 5
3 5
4 8

解释:

$ awk '              # using awk for the job
NR>1 {               # for the records or rows after the first
    print NR-1, $2   # output record number minus one and the second field or column
}' file              # state the file

我希望能够对各种不同的命名列标题执行此操作.使用awk时,您无需指定列标题名称,但可以指定列编号,就像您未声明,但$2.

I would like to be able to do this for various different named column headers. With awk you don't specify the column header name but the column number, like you don't state b but $2.

这篇关于如何使用新行号打印和存储csv文件中的特定命名列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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