保持行以文本文件中的模式开头

时间:2017-07-15 18:00:01

标签: linux awk sed

我正在处理JSON格式的文本文件。我想只保留以这种模式开头的行:

{“MessageType”:“SALES.CONTRACTS.SALECREATED”

在这里您可以看到我的两行数据:

{"MessageType": "SALES.CONTRACTS.HOLDCREATED", "Event": {"Id": "ZWbDoMKQw6HDjFzCo8KuwpNmwofCjl7Co8OPwpDCncOSXMOdccKTZVVWZWbCnA==", "RefInfo": {"TenantId": {"Id": "ZMKXwpbClsOhwpNiw5E="}, "UserId": {"Id": "wpzCksKWwpbCpMKTYsKeZMKZbA=="}, "SentUtc": "2013-04-28T16:59:48.6698042", "Source": 1}, "ItemId": {"Id": 116228}, "Quantity": 1, "ExpirationDate": "2013-04-29T03:59:44.241", "Description": null}}

{"MessageType": "SALES.CONTRACTS.SALECREATED", "Event": {"Id": {"Source": 1, "SourceId": "ZGA=-3-1-002-0801743-2330650"}, "RefInfo": {"TenantId": {"Id": "ZGA="}, "UserId": {"Id": "ZGY="}, "SentUtc": "2013-01-14T15:39:03.5237879", "Source": 1}, "Status": {"Status": 3, "AutoRemoveInfo": null}, "Items": {"Items": [{"Id": {"Id": 1193}, "Sku": {"Sku": "Con BM20"}, "Quantity": 1, "UnitPrice": {"amount": 11.92, "currency": 840}}], "FulfilledItems": []}, "ShippingInfo": {"Carrier": "", "Class": "", "Region": null, "Country": 0, "PostalCode": null, "Costs": null, "Charges": null}, "SaleDate": "2013-01-13T13:39:57", "Kits": null, "Products": null, "AdditionalSaleInfo": null}}

我想要的输出是:

{"MessageType": "SALES.CONTRACTS.SALECREATED", "Event": {"Id": {"Source": 1, "SourceId": "ZGA=-3-1-002-0801743-2330650"}, "RefInfo": {"TenantId": {"Id": "ZGA="}, "UserId": {"Id": "ZGY="}, "SentUtc": "2013-01-14T15:39:03.5237879", "Source": 1}, "Status": {"Status": 3, "AutoRemoveInfo": null}, "Items": {"Items": [{"Id": {"Id": 1193}, "Sku": {"Sku": "Con BM20"}, "Quantity": 1, "UnitPrice": {"amount": 11.92, "currency": 840}}], "FulfilledItems": []}, "ShippingInfo": {"Carrier": "", "Class": "", "Region": null, "Country": 0, "PostalCode": null, "Costs": null, "Charges": null}, "SaleDate": "2013-01-13T13:39:57", "Kits": null, "Products": null, "AdditionalSaleInfo": null}}

使用awk,sed或grep,我如何搜索整个data.raw文件以仅保留以所需模式开头的行?我想将结果保存在new.raw文件中。

2 个答案:

答案 0 :(得分:0)

简单 awk 方法:

awk '/"MessageType": "SALES\.CONTRACTS\.SALECREATED"/' inputfile > newfile

sed 方法:

sed '/"MessageType": "SALES\.CONTRACTS\.SALECREATED"/!d' inputfile > newfile

答案 1 :(得分:0)

尝试一种更多的sed方法。

sed -n '/{"MessageType": "SALES.CONTRACTS.SALECREATED"/p'   Input_file

因此,只需使用-n选项停止打印线条,然后搜索您给出的图案,应该打印并仅打印包含该图案的那条线。

我也尝试过简单的grep,它也成功地为我工作如下。

grep '{"MessageType": "SALES.CONTRACTS.SALECREATED"'  Input_file