1

I have a pattern that looks something like this.

 No Type  Pid    Status  Cause Start Rstr  Err Sem Time Program          Cl  User         Action                    Table
-------------------------------------------------------------------------------------------------------------------------------
 0 DIA    10897 Wait          yes   no     0   0    0                                    NO_ACTION
 1 DIA    10903 Wait          yes   no     0   0    0                                    NO_ACTION
 2 DIA    10909 Wait          yes   no     0   0    0                                    NO_ACTION
 3 DIA    10916 Wait          yes   no     0   0    0                                    NO_ACTION
 4 DIA    10917 Wait          yes   no     0   0    0                                    NO_ACTION
 5 DIA     9061 Wait          yes   no     1   0    0                                    NO_ACTION
 6 DIA    10919 Wait          yes   no     0   0    0                                    NO_ACTION
 7 DIA    10920 Wait          yes   no     0   0    0                                    NO_ACTION
 8 UPD    10921 Wait          yes   no     0   0    0                                    NO_ACTION
 9 BTC    24376 Wait          yes   no     0   0    0                                    NO_ACTION
10 BTC    25651 Wait          yes   no     1   0    0                                    NO_ACTION
11 BTC    25361 Wait          yes   no     0   0    0                                    NO_ACTION
12 BTC    15201 Wait          yes   no     0   0    0                                    NO_ACTION
13 BTC     5241 Wait          yes   no     0   0    0                                    NO_ACTION
14 BTC    23572 Wait          yes   no     0   0    0                                    NO_ACTION
15 BTC     8603 Wait          yes   no     0   0    0                                    NO_ACTION
16 BTC     1418 Wait          yes   no     0   0    0                                    NO_ACTION
17 BTC    18127 Wait          yes   no     1   0    0                                    NO_ACTION
18 BTC    14780 Wait          yes   no     0   0    0                                    NO_ACTION
19 BTC    18234 Wait          yes   no     0   0    0                                    NO_ACTION
20 BTC    14856 Wait          yes   no     0   0    0                                    NO_ACTION
21 SPO    10934 Wait          yes   no     0   0    0                                    NO_ACTION
22 UP2    10939 Wait          yes   no     0   0    0                                    NO_ACTION

Now I am using awk to convert it something like below

NO=0,Type=DIA,Pid=10897,Status=Wait,Cause=yes,Start=no,Rstr=0,Err=0,Sem=0,Time=NO_ACTION,Program=,Cl=,User=,Action=,Table=

above is the sample for one line would be the same for all lines.

we are removing the column header via sed command at runtime, now when we use awk it misses space between status and cause and write the value which should be with start at the cause.

we are using the below command.

awk 'BEGIN{FS=" ";OFS=","}{print "NO="$1,"Type="$2,"Pid="$3,"Status="$4,"Cause="$5,"Start="$6,"Rstr="$7,"Err="$8,"Sem="$9,"Time="$10,"Program="$11,"Cl="$12,"User="$13,"Action="$14,"Table="$15;}'

we want the output to be like this

NO=0,Type=DIA,Pid=10897,Status=Wait,Cause=,Start=yes,Rstr=no,Err=0,Sem=0,Time=0,Program=,Cl=,User=,Action=NO_ACTION,Table=

and one more thing to add these blank fields will have some values from time to time.

  • is this file separated with tab or space? – msp9011 Jul 17 at 9:48
  • How to check that, above is the output of a command – amit singh Jul 17 at 12:10
  • the character between each column is a single space or a tab... – msp9011 Jul 17 at 12:17
  • 1
    msp9011 is right. That's a space-separated file, so the empty fields (Cl, User, Table) cannot be processed correctly since there is no way of knowing they are empty. If you had used tabs as the separator, any case where we had \t\t, two consecutive tabs, would indicate an empty field. But in a file with multiple spaces like this one, there is no way of differentiating between an empty field and consecutive spaces put there only to make the file look pretty. If this file was creating by exporting data from another program, try exporting the data again but using another field separator. – terdon Jul 17 at 12:31
  • Are you able to guarantee that the columns start and end at fixed character positions within each line? – Kusalananda Jul 18 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.