bash的方法去除csv文件最后4列 [英] bash method to remove last 4 columns from csv file

查看:165
本文介绍了bash的方法去除csv文件最后4列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用bash删除最后四列某些输入CSV文件的方法吗?最后四列可以有在长度上变化,从线到线字段所以它不足以只是从各行的末端删除一定数目的字符。

Is there a way to use bash to remove the last four columns for some input CSV file? The last four columns can have fields that vary in length from line to line so it is not sufficient to just delete a certain number of characters from the end of each row.

推荐答案

如果你不削减可以做到这一点,如果所有行具有相同的字段数和awk。

Cut can do this if all lines have the same number of fields or awk if you don't.

cut -d, -f1-6 # assuming 10 fields

将打印出的第6场,如果你想控制输出seperater使用--output分隔符=字符串

Will print out the first 6 fields if you want to control the output seperater use --output-delimiter=string

awk -F , -v OFS=, '{ for (i=1;i<=NF-4;i++){ printf $i, }; printf "\n"}'

遍历领域多达日字段数-4并打印出来。

Loops over fields up to th number of fields -4 and prints them out.

这篇关于bash的方法去除csv文件最后4列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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