簡體   English   中英

如何在bash中查找和打印特定字符

[英]How to find and print specific character in bash

我有這樣的文件:

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

我不知道行中是否缺少A,B,C或D值。 但是我需要像這樣轉換該文件:

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

因此,如果缺少任何值,則僅打印-標記。 我的計划是讓相同數量的列易於解析。 我更喜歡awk解決方案。 感謝您的任何建議或幫助。

我的第一次嘗試是:

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

但隨后我注意到,某些值丟失了。

編輯:

從我的標頭中,我知道值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,分割輸入行,並保存到@F數組
  • -l從輸入行中刪除換行符,向輸出中添加換行符
  • @k=(A..F); AB等初始化@k數組直到F
  • $op[0]=$F[0]; @op[1..6]=("-")x6; @F第一個元素@op數組,其余六個元素為-
  • for循環遍歷@F數組,如果元素與相應索引中的@k數組元素匹配,后跟= ,則更改@op元素
  • print join(",",@op)打印@op與陣列,作為分隔符

Perl進行救援!

您尚未指定如何獲取標頭信息,因此在以下腳本中,直接填充@header數組。

%to_idx哈希將列名映射到其索引(A => 0,B => 1等)。

每行都分成多個字段,將每個字段與期望的字段( $next )比較,並在需要時打印破折號。 缺少尾隨字段也會發生同樣的情況。

#!/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"
}

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)

跑:

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

對於記錄中未找到的每個字母,它將打印“-”。 如果數據具有標題,則可以split為二維數組b[NR]並將for(i in a)更改for(i in b[1]) ... printf ... b[NR][b[1][i]] ... ,如果不需要靜態的第一列,請刪除第一printfdelete

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM