-2

I have this file inside a mariaDB that looks like this

name    callerid    secret  context type    host
1000    Omar Al-Ani <1000>  op1000DIR   MANAGEMENT  friend  dynamic 
1001    Ammar Zigderly <1001>   1001    MANAGEMENT  peer    dynamic 
1002    Lubna COO Office <1002>     1002    ELdefault   peer    dynamic

i want to convert it using sed and awk to look like this format

[1000]
        callerid=Omar Al-Ani <1000>
        secret=op1000DIR
        context=MANAGEMENT
        type=friend
        host=dynamic
[1001]
        callerid=Ammar Zigderly <1001>
        secret=1001
        context=MANAGEMENT
        type=peer
        host=dynamic

[1002]
        callerid=Lubna COO Office <1002>
        secret=1002
        context=ELdefault
        type=peer
        host=dynamic

This is the output of this command head -3 filename | od -c on the input file

  0000000   n   a   m   e  \t   c   a   l   l   e   r   i   d  \t   s   e
0000020   c   r   e   t  \t   c   o   n   t   e   x   t  \t   t   y   p
0000040   e  \t   h   o   s   t  \n   1   0   0   0      \t   O   m   a
0000060   r       A   l   -   A   n   i       <   1   0   0   0   >    
0000100  \t   o   p   1   0   0   0   D   I   R      \t   M   A   N   A
0000120   G   E   M   E   N   T  \t   f   r   i   e   n   d  \t   d   y
0000140   n   a   m   i   c      \n   1   0   0   1      \t   A   m   m
0000160   a   r       Z   i   g   d   e   r   l   y       <   1   0   0
0000200   1   >      \t   1   0   0   1      \t   M   A   N   A   G   E
0000220   M   E   N   T  \t   p   e   e   r  \t   d   y   n   a   m   i
0000240   c      \n
0000243

Any idea would be helpfull !

resha
  • 78
  • 1
  • 7
  • Thanks for sharing your efforts. Could you please elaborate more on how you are getting output of `5701` and `context=SupportGroup` getting? Can't see them in input, thank you. – RavinderSingh13 Oct 13 '22 at 09:46
  • 5701 is the name context is shown in the input : MANAGEMENT or ELdefault – resha Oct 13 '22 at 10:33
  • Please describe what `-e '//s/.//'` is supposed to do – Daweo Oct 13 '22 at 11:48
  • 1
    please update the question to show fewer inputs (eg, 5 lines) and then update the expected output to show the results for those 5 lines of input; as currently written there doesn't appear to be any relationship between the sample input and the expected output – markp-fuso Oct 13 '22 at 13:06
  • 1
    does the input contain the header record (`name`, `callerid`, `secret`, `context`, `type`, `host`) and will the columns of input always be in the same order? please update the question to include details on the column delimiter (right now it looks like white space but there's no way to distinguish spaces as column delimiters and spaces as part of the the data); is `username` (output) supposed to map to the `name` (input)? – markp-fuso Oct 13 '22 at 13:10
  • @markp-fuso yes you are right there is no relation , but it's the same example only with diffrenet names and number – resha Oct 13 '22 at 14:07
  • @markp-fuso yes always in the same order , username is mapped to name , i'm using vim so it's a tab space and it's not important as we can modify it later – resha Oct 13 '22 at 14:10
  • 2
    and how are we supposed to know if we've generated the correct output if we have nothing to compare it against? knowing the delimiters *are* important as that is going to determine how we parse the input; you're glossing over details that *are* required if you expect a useful answer – markp-fuso Oct 13 '22 at 14:10
  • 2
    @markp-fuso, exactly. I was trying to ask to OP. – RavinderSingh13 Oct 13 '22 at 14:10
  • I will edit it to match the i/o – resha Oct 13 '22 at 14:28
  • @RavinderSingh13 Please check now , it should be more clear – resha Oct 13 '22 at 14:33
  • @markp-fuso please check now – resha Oct 13 '22 at 14:34
  • 1
    we still need to know the column delimiter; please update the question with the output from running `head -3 filename | od -c` – markp-fuso Oct 13 '22 at 14:37
  • @markp-fuso Done , please check – resha Oct 13 '22 at 14:41
  • The file containes over 2k lines so this is only a sample , but everything is similar – resha Oct 13 '22 at 14:42

2 Answers2

1

I think awk is going to be a bit simpler and easier (?) to modify if requirements change:

awk -F'\t' '

BEGIN { labels[2]="callerid"
        labels[3]="secret"
        labels[4]="context"
        labels[5]="type"
        labels[6]="host"
      }

FNR>1 { gsub(/ /,"",$1)                             # remove spaces from 1st column
        printf "[%s]\n",$1
        for (i=2;i<=6;i++)
            printf "\t%s=%s\n", labels[i],$i
        print ""
      }
' names.dat

This generates:

[1000]
        callerid=Omar Al-Ani <1000>
        secret=op1000DIR
        context=MANAGEMENT
        type=friend
        host=dynamic

[1001]
        callerid=Ammar Zigderly <1001>
        secret=1001
        context=MANAGEMENT
        type=peer
        host=dynamic

[1002]
        callerid=Lubna COO Office <1002>
        secret=1002
        context=ELdefault
        type=peer
        host=dynamic
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • This is great ! only one issue left , many outputs appera as this [3403 ] as you can see there is an extra space after the last number .. how can we solve this ? – resha Oct 13 '22 at 14:57
  • @resha I missed the trailing space in the `od -c` output; I've added a `gsub()` call to remove all spaces from the 1st column – markp-fuso Oct 13 '22 at 15:00
  • 1
    hmmm, looks like you've got trailing spaces in some of the other columns ... they are there in the output but not visually obvious; if you *need* to remove the trailing spaces there are a few ways to use `gsub()` (eg, [this answer](https://stackoverflow.com/a/33380957)) ... if you have problems trimming trailing spaces then consider asking a new question; regards – markp-fuso Oct 13 '22 at 15:10
1

assuming tab separated fields

$ awk -F'\t' 'NR==1 {split($0,h); next} 
                    {print "[" $1 "]"; 
                     for(i=2;i<=NF;i++) print "\t" h[i] ":" $i}' file.tcv

[1000]
        callerid:Omar Al-Ani <1000>
        secret:op1000DIR
        context:MANAGEMENT
        type:friend
        host:dynamic 
[1001]
        callerid:Ammar Zigderly <1001>
        secret:1001
        context:MANAGEMENT
        type:peer
        host:dynamic 
[1002]
        callerid:Lubna COO Office <1002>
        secret:1002
        context:ELdefault
        type:peer
        host:dynamic
karakfa
  • 66,216
  • 7
  • 41
  • 56