AWK - 如何删除与场分离第一列(awk - how to delete first colum

2019-09-02 06:58发布

我有带有数据的csv文件如下

87540221|1356438283301|1356438284971|1356438292151697
87540258|1356438283301|1356438284971|1356438292151697
87549647|1356438283301|1356438284971|1356438292151697

我想的第一列保存到一个新文件(无场分离,然后用第一场分离器沿删除主CSV文件中的第一列。

有任何想法吗?

这是我到目前为止已经试过

awk 'BEGIN{FS=OFS="|"}{$1="";sub("|,"")}1'

但它不工作

Answer 1:

假设你原来的CSV文件名为“orig.csv”:

awk -F'|' '{print $1 > "newfile"; sub(/^[^|]+\|/,"")}1' orig.csv > tmp && mv tmp orig.csv


Answer 2:

这是简单的cut

$ cut -d'|' -f1 infile
87540221
87540258
87549647

$ cut -d'|' -f2- infile
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697

只需重新引导到你想要的文件:

$ cut -d'|' -f1 infile > outfile1

$ cut -d'|' -f2- infile > outfile2 && mv outfile2 file 


Answer 3:

管道是特殊的正则表达式符号和子功能expectes你传递一个正则表达式。 正确的awk命令应该是这样的:

awk 'BEGIN {FS=OFS="|"} {$1=""; sub(/\|/, "")}'1 file

OUTPUT:

1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697


Answer 4:

随着sed

sed 's/[^|]*|//' file.txt


Answer 5:

GNU AWK

awk '{$1="";$0=$0;$1=$1}1' FPAT='[^|]+' OFS='|'

产量

1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697
1356438283301|1356438284971|1356438292151697


文章来源: awk - how to delete first column with field separator
标签: linux shell awk