处理文本文件 - 提取以数字开头的数据

处理文本文件 - 提取以数字开头的数据

我有一个需要处理的联系信息列表,其.txt格式由 分隔,。我们只想保留以65房地产开头的 SIC 代码。

该命令应该只检查以 65 开头的数据的正确字段。

请记住,该数字并不总是6531,它只需以 开头65(例如 6521 6555 6587 也是我们想要保留的内容)

2,J,John Foraste Photography,[email protected],68 Middle Hwy,Barrington,RI,2806, , ,733511,Photographic Engineering,atlanticinn.com
3,X,Xerox Corp,[email protected],10 Orms St # 420,Providence,RI,02904-7815,5594547871,4012763242,504403,Copying & Duplicating Machines & Supls,www.xerox.com
4,S,St Sahag & St Mesrob Armenian,[email protected],70 Jefferson St,Providence,RI,02908-4923,4012722832,4012727712,866107,Churches,www.stsahmes.org
13,C,Century 21 Access America,[email protected],1025 Tiogue Ave,Coventry,RI,02816-6100,4018282100, ,6531,Real Estate, 
14,B,Baxter's Showroom,[email protected],Null,Warwick,RI,0,4017398222,4017397058,594409,Jewelers,baxtersjewelry.com^^majorfindings.com^^robertbaxter.com^^san
17,R,Re/Max South County,[email protected],56 Wells Street,Westerly,RI,2891,4015962067, ,6531,Real Estate, 
19,L,Lyn Reale - Block Island Realty,[email protected],215Chapelstreet,Block Island,RI,2807,4012534311, ,653118,Real Estate,stmichaelsbristolri.org
21,R,Re/Max South County,[email protected],56 Wells Street,Westerly,RI,2891,4015962067, ,6531,Real Estate, 
22,V,Vns Home Health Svc,[email protected],14 Woodruff Ave # 7,Narragansett,RI,02882-3467,4017882253,4017820500,808201,Home Health Service,

处理后的列表应该是

13,C,Century 21 Access America,[email protected],1025 Tiogue Ave,Coventry,RI,02816-6100,4018282100, ,6531,Real Estate, 
17,R,Re/Max South County,[email protected],56 Wells Street,Westerly,RI,2891,4015962067, ,6531,Real Estate, 
19,L,Lyn Reale - Block Island Realty,[email protected],215Chapelstreet,Block Island,RI,2807,4012534311, ,653118,Real Estate,stmichaelsbristolri.org
21,R,Re/Max South County,[email protected],56 Wells Street,Westerly,RI,2891,4015962067, ,6531,Real Estate, 

答案1

awk

awk -F, '{if ( $11 ~ /^65/ ) print $0}' file

说明:使用逗号作为字段分隔符-F,,检查第 11 列是否^以 65 开头()if ( $11 ~ /^65/ ),如果是则打印整行print $0

答案2

这应该可以做到:

#!/usr/bin/env perl

use strict;
use warnings;

while ( <> ) {
    print if (split /,/)[10] =~ m/^65/;
}

如果您愿意,可以将其衬垫为:

perl -ne 'print if (split /,/)[10] =~ m/^65/;' yourfile

答案3

sed '/,65[0-9]*,Real Estate/! d' file.txt

相关内容