如何在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的值。但是我需要像这样转换这个文件:
pre $ code> AA,A = 14,B = 356,C = 845,D = 4516 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}'
但是我注意到,有些值是缺失的。 b
$ b
编辑:
从我的标题我知道有价值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] =(split //, - x6);
$ j = 0;对于($ i = 1; $ i <= $#F;){if($ F [$ i] =〜m / $ k [$ j ++] = /){$ op [$ j] = $ F [$一世]; $ 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, code>在
,
上分割输入行并保存到@F
数组<$ li $-l </ code>从输入行中删除换行符,将换行符添加到输出中
@ k =(A ..);
使用A
,初始化
等等upto@k
> BF
$ op [0] = $ F [0]; @op [1..6] =(split //, - x6) initalize
和其余六个元素作为@op
第一个元素为<$ c的数组$ c> @F-
- for循环遍历
@F
数组,如果元素匹配@k
相应索引中的数组元素,后跟=
,改变@op
元素
print join(,,@ op)
用,
作为分隔符打印@op
数组
无法在 awk
中实现类似的逻辑(还不完全熟悉语法和结构)。
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...
prompt# 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
prompt# perl -F, -le '@k=(A..F);
$op[0]=$F[0]; @op[1..6]=(split //,"-"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 withA
,B
, etc uptoF
$op[0]=$F[0]; @op[1..6]=(split //,"-"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
Couldn't implement similar logic in awk
(not yet fully familiar with syntax and constructs).
这篇关于如何在bash中找到并打印特定的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!