简体   繁体   English

在冒号后使用简短的单个命令行(如文本文件中的awk,sed或perl)替换特定值

[英]Replace specific values after colon using a short single command line like awk, sed or perl in text file

I tried looking at many questions but I still couldn't create some awk command for that. 我尝试查看了许多问题,但仍然无法为此创建一些awk命令。 Basically I have a file with space indentation (not tabs) and I'd like to change just the values of the fields price, quantity and symbol for the value "[scrub]". 基本上,我有一个带有空格缩进的文件(不是制表符),我只想更改价格“ [scrub]”的价格,数量和符号等字段的值。 How can I do this with a single line command maintaining the format and the other values of the file? 如何通过单行命令维护文件的格式和其他值?

From: 从:

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

To: 至:

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]

Thanks in advance! 提前致谢!

Using just for price for example : 例如,仅将用于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 

For the rest : 对于其余的 :

$ 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

In the end I created a script because the command was too big. 最后,由于命令太大,我创建了一个脚本。 I'm putting here just for information: 我在这里仅供参考:

#!/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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM