下面是我的 csv 文件,我想从retailer_id 字段中删除所有出现的 - 并创建新的 csv。
>IPAY_USER_ID,RETAILER_ID,CUST_FIRST_NAME,CUST_LAST_NAME,CUST_MIDDLE_NAME,ACTIVATION_ACTOR_ID,DATE_OF_BIRTH,GENDER,EMAIL_ID,MOBILE_NO,CUSTOMER_CATEGORY,CUST_STATUS,WALLET_TYPE,MOBILE_CIRCLE,MPIN_EXPRY_DATE,R_MOD_ID,R_MOD_TIME,R_CRE_ID,CREATION_DATE,CREATION_TIME,RETAILER_UPGRADE_REG_DATE,RETAILER_UPGRADE_REG_TIME,DEDUP2_DATE,DEDUP2_TIME,DATA_ENRICHMENT_DATE,DATA_ENRICHMENT_TIME,BLACKLIST_DATE,BLACKLIST_TIME,DEDUP3_DATE,DEDUP3_TIME,KYCN_P_Registration_Mode,CHANNEL,TD_PD_STATUS,DEFAULT_MPIN_CHANGED_OR_NOT,UPGRADE_CHANNEL,UPGRADE_STATUS,LAST_TXN_DATE,KYCF_CONVERSION_DATE,KYCF_CONVERSION_TIME,NOMINEE_NAME,RELATION_CODE,BALANCE,SEEDING AUTHORISATION ID
22909943,--,RAL,WAL,,0,08/jan/1997,,[email protected],9923,,ACTIVE,NOKYC,RJ,2025-08-27 21:19:30,22909943,2015-11-05 17:21:17,22909943,2015-08-27,21:19:30,,,,,,,2015-11-05,17:21:17,,,SELF,WEB,,-,,PENDING,2015-08-27 21:19:30,,,,,0,
答案1
awk -F , -v OFS=, '{gsub(/-/, "", $2); print}' < in.csv > out.csv
答案2
sed -i 's/--//g' in.cvs > out.cvs
答案3
我会用sed
它。
$ sed -r -i 's/^([0-9]+,)--,/\1,/g' file.csv
不过,我喜欢 Stéphane 的回答。RETAILER_ID
例如,如果该字段是第十个字段,则正则表达式 tosed
会更难看。
答案4
我可能会在 perl 中执行此操作,因为它允许您对命名字段进行选择:
#!/usr/bin/env perl
use strict;
use warnings;
#read header row from "STDIN" (or file on command line);
chomp ( my @header = split /,/, <> );
#print it
print join ",", @header, "\n";
#iterate STDIN or file on command line - line by line
while ( <> ) {
#declare a row
my %this_row;
#strip trailing linefeed (optional, given you need to reinsert it)
chomp;
#select fields in this row, into named fields based on the header row.
@this_row{@header} = split /,/;
#apply regex to just RETAILER_ID
$this_row{'RETAILER_ID'} =~ s/--//;
#print row. map is unnecessary if you've always got a full set of fields.
#I've included it because your sample data didn't.
print join ( "," , map { $_ // '' } @this_row{@header} ),"\n";
}
它比单行长一点sed
- 但如果您愿意,您可以将其单行化。
因为这个脚本使用<>
- 这是神奇的文件句柄,读取任何一个STDIN 或在命令行上指定的文件,就像 等grep
一样sed
。但这意味着,perl -i
如果这是您的目标,您可以进行并就地编辑。或者只是重定向输出。