bash 删除csv文件的第一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46904805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:32:50  来源:igfitidea点击:

Delete first column of csv file

bashawksed

提问by Stoufiler

I would like to know how i can delete the first column of a csv file with awk or sed

我想知道如何使用 awk 或 sed 删除 csv 文件的第一列

Something like this :

像这样的事情:

FIRST,SECOND,THIRD

To something like that

对这样的事情

SECOND,THIRD

Thanks in advance

提前致谢

回答by RavinderSingh13

Following awk will be helping you in same.

遵循 awk 将同样帮助你。

awk '{sub(/[^,]*/,"");sub(/,/,"")} 1'   Input_file

Following sed may also help you in same.

跟随 sed 也可以帮助你。

sed 's/\([^,]*\),\(.*\)//'  Input_file

Explanation:

解释:

awk '                 ##Starting awk code here.
{
  sub(/[^,]*/,"")     ##Using sub for substituting everything till 1st occurence of comma(,) with NULL.
  sub(/,/,"")         ##Using sub for substituting comma with NULL in current line.
}
1                     ##Mentioning 1 will print edited/non-edited lines here.
'   Input_file        ##Mentioning Input_file name here.

回答by Darby_Crash

With Sed

使用 Sed

sed -i -r 's@^\w+,@@g' test.csv

Grab the begin of the line ^, every character class [A-Za-z0-9] and also underscore until we found comma and replace with nothing. Adding g after delimiters you can do a global substitution.

抓住 ^ 行的开头,每个字符类 [A-Za-z0-9] 并下划线,直到我们找到逗号并替换为空。在分隔符后添加 g 您可以进行全局替换。

回答by Rahul Verma

Using awk

使用awk

$awk -F, -v OFS=, '{=; =; NF--;}1' file
SECOND,THIRD

回答by P....

Using sed: ^[^,]+,regex represent the first column including the first comma. ^means start of the line, [^,]+,means anything one or more times but a comma sign followed by a comma.

使用sed: ^[^,]+,regex 表示包括第一个逗号的第一列。^表示行的开头,[^,]+,表示任何一次或多次,但逗号符号后跟逗号。

you can use -iwith sedto make changes in file if needed.

如果需要,您可以使用-iwithsed在文件中进行更改。

sed -r 's/^[^,]+,//' input
SECOND,THIRD