Home > Blockchain >  Remove matching character in the first line and all characters below in other lines
Remove matching character in the first line and all characters below in other lines

Time:05-31

I have some strings

M-AA-
-AG--
M---G

I want to remove - in the first line and all characters corresponding to the position of the - in other lines, to get

MAA
-G-
M--

May I know how to do it in the simplest bash command?

CodePudding user response:

With awk [1], if you set the field separator to the empty string, you can iterate over each character:

awk '
    BEGIN {FS = OFS = ""}
    NR == 1 {for (i=1; i<=NF; i  ) if ($i == "-") remove[i]}
    {for (i in remove) $i = ""; print}
' file

[1] - at least gawk, mawk and the default awk on recent MacOS.

  •  Tags:  
  • bash
  • Related