简体   繁体   中英

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. 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]". 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 :

$ 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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