[英]How to replace string after colon using sed, awk, regex, yq etc and can run using shell script
[英]Replace specific values after colon using a short single command line like awk, sed or perl in text file
我尝试查看了许多问题,但仍然无法为此创建一些awk命令。 基本上,我有一个带有空格缩进的文件(不是制表符),我只想更改价格“ [scrub]”的价格,数量和符号等字段的值。 如何通过单行命令维护文件的格式和其他值?
从:
Values:
Client Order ID : new000000000001
Client Quote ID : N/A
Exchange Quote ID : N/A
Symbol : EUR/USD
Spot attributes
Price : $150.00000000 : 15000000000
Discretion : $0.00000000 : 0
Quantity : 100
至:
Values:
Client Order ID : new000000000001
Client Quote ID : N/A
Exchange Quote ID : N/A
Symbol : [scrub]
Spot attributes
Price : [scrub]
Discretion : $0.00000000 : 0
Quantity : [scrub]
提前致谢!
例如,仅将perl用于price
:
$ perl -pe 's/^(\s+Price\s+:\s+).*/$1\[scrub\]/g' prices
Values:
Client Order ID : new000000000001
Client Quote ID : N/A
Exchange Quote ID : N/A
Symbol : EUR/USD
Spot attributes
Price : [scrub]
Discretion : $0.00000000 : 0
Quantity
对于其余的 :
$ perl -pe '
s/^(\s+Price\s+:\s+).*/$1\[scrub\]/g;
s/^(\s+Symbol\s+:\s+).*/$1\[scrub\]/g;
s/^(\s+Quantity\s+:\s+).*/$1\[scrub\]/g;
' file
最后,由于命令太大,我创建了一个脚本。 我在这里仅供参考:
#!/usr/bin/env perl
use strict;
use warnings;
open INPUTFILE, "<", $ARGV[0] or die $!;
open OUTPUTFILE, ">", $ARGV[1] or die $!;
while (<INPUTFILE>) {
s/^(\s+Price\s+:\s+).*/$1\[scrub\]/g;
s/^(\s+Symbol\s+:\s+).*/$1\[scrub\]/g;
s/^(\s+Quantity\s+:\s+).*/$1\[scrub\]/g;
s/^(\s+FIX content drop copy only\s+:\s+).*/$1\[scrub\]/g;
print OUTPUTFILE;
}
close OUTPUTFILE;
close INPUTFILE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.