简体   繁体   中英

How to find and print specific character in bash

I have file like:

AA,A=14,B=356,C=845,D=4516
BB,A=65,C=255,D=841,E=5133,F=1428
CC,A=88,B=54,C=549,F=225

I never know if in the row missing A,B,C or D value. But I need to transform this file like:

AA,A=14,B=356,C=845,D=4516,-,-
BB,A=65,-,C=255,D=841,E=5133,F=1428
CC,A=88,B=54,C=549,-,-,F=225

So if any value missing print just - mark. My plan is have the same number of columns to easy parsing. I am prefer awk solution. Thank you for any advice or help.

My first try was:

awk '{gsub(/[,]/, "\t")}; BEGIN{ FS = OFS = "\t" } { for(i=1; i<=NF; i++) if($i ~ /^ *$/) $i = "-" }; {print $0}'

But then I notice, that some values are missing.

EDIT:

From my header I know that there is value A,B,C,D,E,F...

$ cat file.txt
AA,A=14,B=356,C=845,D=4516
BB,A=65,C=255,D=841,E=5133,F=1428
CC,A=88,B=54,C=549,F=225

$ perl -F, -le '@k=(A..F);
   $op[0]=$F[0]; @op[1..6]=("-")x6;
   $j=0; for($i=1;$i<=$#F;){ if($F[$i] =~ m/$k[$j++]=/){$op[$j]=$F[$i]; $i++} }
   print join(",",@op)
   ' file.txt
AA,A=14,B=356,C=845,D=4516,-,-
BB,A=65,-,C=255,D=841,E=5133,F=1428
CC,A=88,B=54,C=549,-,-,F=225
  • -F, split input line on , and save to @F array
  • -l removes newline from input line, adds newline to output
  • @k=(A..F); initialize @k array with A , B , etc upto F
  • $op[0]=$F[0]; @op[1..6]=("-")x6; initalize @op array with first element of @F and remaining six elements as -
  • for-loop iterates over @F array, if element matches with @k array element in corresponding index followed by = , change @op element
  • print join(",",@op) print the @op array with , as separator

Perl to the rescue!

You haven't specified how to obtain the header information, so in the following script, the @header array is populated directly.

%to_idx hash maps the column names to their indices (A => 0, B => 1 etc.).

Each lines is split into fields, each field is compared to the expected one ( $next ) and dashes are printed if needed. The same happens for missing trailing fields.

#!/usr/bin/perl
use warnings;
use strict;

my @header = qw( A B C D E F );

my %to_idx = map +($header[$_] => $_), 0 .. $#header;

open my $IN, '<', shift or die $!;
while (<$IN>) {
    chomp;
    my @fields = split /,/;
    print shift @fields;
    my $next = 0;
    for my $field (@fields) {
        my ($name, $value) = split /=/, $field;
        print ',-' x ($to_idx{$name} - $next);
        print ",$name=$value";
        $next = $to_idx{$name} + 1;
    }
    print ',-' x (1 + $#header - $next);  # Missing trailing fields.
    print "\n"
}

Solution in TXR

@(do
   (defstruct fill-missing nil
     strings
     (hash (hash :equal-based))

     (:postinit (self)
       (each ((s self.strings))
         (set [self.hash s] "-")))

     (:method add (self str val)
       (set [self.hash str] `@str=@val`))

     (:method print (self stream)
       (put-string `@{(mapcar self.hash self.strings) ","}` stream))))
@(repeat)
@  (bind fm @(new fill-missing strings '#"A B C D E F"))
@{label},@(coll)@{sym /[^,=]+/}=@{val /[^,]+/}@(do fm.(add sym val))@(end)
@  (do (put-line `@label,@fm`))
@(end)

Run:

$ txr missing.txr data
AA,A=14,B=356,C=845,D=4516,-,-
BB,A=65,-,C=255,D=841,E=5133,F=1428
CC,A=88,B=54,C=549,-,-,F=225
BEGIN {                                  
    PROCINFO["sorted_in"]="@ind_str_asc" # order for for(i in a)
    for(i=65;i<=90;i++)                  # create the whole alphabet to array a[]
        a[sprintf("%c", i)]              # you could read the header and use that as well
}
{
    split($0,b,",")                      # split record by ","
    printf "%s", b[1]                    # printf first element (AA, BB...)
    delete b[1]                          # get rid of it
    for(i in b) 
        b[substr(b[i],1,1)]=b[i]         # take the first letter to use as index (A=12)
    for(i in a)                          # go thru alphabet and printf from b[]
        printf "%s%s", OFS, (i in b?b[i]:"-"); print ""
}

awk -v OFS=\, -f parsing.awk tbparsed.txt
AA,A=14,B=356,C=845,D=4516,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-
BB,A=65,-,C=255,D=841,E=5133,F=1428,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-
CC,A=88,B=54,C=549,-,-,F=225,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-

It prints "-" for each letter not found in the record. If the data had a header, you could split to 2-D array b[NR] and change the for(i in a) to for(i in b[1]) ... printf ... b[NR][b[1][i]] ... and if you don't need the static first column, remove the first printf and delete .

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