根据某些条件将 CSV 文件拆分为更小的文件

根据某些条件将 CSV 文件拆分为更小的文件

我有一个包含以下值的文件:

Server1,12.22.21.13,1234,[email protected]
Server2,12.12.12.12,1223,[email protected]
Server3,13.11.11.11,1234,[email protected]
Server4,11.11.11.11,1234,[email protected]

我想根据电子邮件地址拆分文件。所有包含电子邮件的行[电子邮件受保护]应该出现在一个名为文件1等等。

这是一个非常大的文件,我无法在案例声明中对所有电子邮件 ID 进行硬编码。

答案1

(呆呆地)

awk -F"," '{ print $0 >> $4".csv"}' filename

解释:

-F"," -- Use "comma" field separator (FS) 
$0 -- the whole line of input
$4 -- the 4-th field of line ($NF -- the last field)
>> -- redirection operator, from `info gawk':

'print ITEMS >> OUTPUT-FILE'
  This redirection prints the items into the preexisting output file
  named OUTPUT-FILE.  The difference between this and the single-'>'
  redirection is that the old contents (if any) of OUTPUT-FILE are
  not erased.  Instead, the 'awk' output is appended to the file.  If
  OUTPUT-FILE does not exist, then it is created.

相关内容