Appending column to rows

[LEFT]Hi All,
Input.txt[/LEFT]

KGO
  Id "003"
  ..........
  ..........
     Par "CPara"
     BIN RECGET
        Name "DIR_PATH"
        Prompt "DIR_PATH"
     END RECGET
KGO
............
..........
...............
KGO
  Id "077"
  ..........
  ..........
     Par "CPara"
     BIN RECGET
        Name "FILE"
        Prompt "Enter File"
     END RECGET
KGO
.........
..........
..........

Desired output should be:

Id,Name,Prompt
003,DIR_Path,Enter DIR_Path
007,File,Enter File

I have tried:

$grep -i -e Id -e Name -e Prompt Input.txt | sed '/   //g'
Id "003"
Name "DIR_PATH"
Prompt "DIR_PATH"
Id "077"
Name "FILE"
Prompt "Enter File"

Help me how to achieve this

---------- Post updated at 09:16 PM ---------- Previous update was at 09:08 PM ----------

Got the solution from previous post...

$grep -i -e Id -e Name -e Prompt Input.txt | sed '/   //g' > temp
$paste -sd',,\n' temp
awk -v c="Id Name Prompt" -v OFS=, 'BEGIN {
  n = split(c, t)
  for (i = 0; ++i <= n;) {
    printf "%s", (t (i < n ? OFS : ORS))
    v[t]
    }
  }
$1 in v { get_val($1) }
/END RECGET/ {
  for (i = 0; ++i <= n;)
    printf "%s", (d[t] (i < n ? OFS : ORS))
  }
func get_val(x) {
  match($0, /"[^"]+"/) &&
    r = substr($0, RSTART + 1, RLENGTH - 2)
  d[x] = r 
  }' infile 
1 Like
$awk -F"\"" 'BEGIN{i=1;print "Id,Name,Prompt"} ($1 ~ /Id|Name|Prompt/){if(i==4){print x;i=1};i++;printf $2","}END{print x}' inputfile
1 Like

Thanks radoulov & raj_saini20,
There is some change in requirement: I think raj's code is bit simpler for me.. could you please help me out for the below

KGO
   Id "003"
   ..........
   ..........
      Par "CPara"
   BIN RECGET
         Name "DIR_PATH"
         Prompt "DIR_PATH"
      END RECGET
   BIN RECGET
         Name "DB_SID"
         Prompt "ENTER DB SID NAME"
      END RECGET
   ...........
   ..............
KGO
............
..........
...............
KGO
   Id "077"
   ..........
   ..........
      Par "CPara"
      BIN RECGET
         Name "FILE"
         Prompt "Enter File"
      END RECGET
KGO
.........
..........
..........
 

Outputshoulde be:

Id,Name,Prompt
003,DIR_PATH,Enter DIR_PATH
003,DB_SID,ENTER DB SID NAME
077,FILE,Enter File

This is what I get when I run my code with your sample input:

zsh-4.3.12[t]% awk -v c="Id Name Prompt" -v OFS=, 'BEGIN {
  n = split(c, t)
  for (i = 0; ++i <= n;) {
    printf "%s", (t (i < n ? OFS : ORS))
    v[t]
    }
  }
$1 in v { get_val($1) }
/END RECGET/ {
  for (i = 0; ++i <= n;)
    printf "%s", (d[t] (i < n ? OFS : ORS))
  }
func get_val(x) {
  match($0, /"[^"]+"/) &&
    r = substr($0, RSTART + 1, RLENGTH - 2)
  d[x] = r
  }' infile
Id,Name,Prompt
003,DIR_PATH,DIR_PATH
003,DB_SID,ENTER DB SID NAME
077,FILE,Enter File

Do you need/expect something different?

Thanks for your reply radoulov,
Sorry for confusion you by providing sample data.. below is the original file where I need get only values

Identifier==> "ListFileNames"

Then Name & Prompt between text
Parameters "CParameters" & NLSLocale ",,,,"

   Time
   ServerVersion
  ............
  .........
  ........
BEGIN KGO
   Identifier "ListFileNames"
   DateModified "2012-07-18"
   TimeModified "23.38.04"
   BEGIN RECGET
      Identifier "ROOT"
      DateModified "1899-12-30"
      TimeModified "00.00.01"
      Readonly "0"
      Name "ListFileNames"
      NextID "2"
      Parameters "CParameters"
      BEGIN DSSUBRECORD
         Name "FilePath"
         Prompt "File Path Name"
         ParamType "4"
         ParamLength "0"
         ParamScale "0"
      END DSSUBRECORD
      BEGIN DSSUBRECORD
         Name "FileNamesFileName"
         Prompt "Enter the File Names File Name"
         ParamType "4"
         ParamLength "0"
         ParamScale "0"
      END DSSUBRECORD
      MetaBag "CMetaProperty"
      NULLIndicatorPosition "0"
      IsTemplate "0"
      NLSLocale ",,,,"
      JobType "0"
      Name "Job"
      NextID "1"
   BEGIN RECGETECORD
      Identifier "V1S1"
      DateModified "1899-12-30"
      TimeModified "00.00.01"
      OLEType "CSeqFileStage"
      Readonly "0"
      Name "FileNamesFile"
   END RECGET
 
END KGO
 
BEGIN KGO
Identifier "Danny"
..........
........
END KGO
.........
........

my putput should be:

  Identifier,Name,Prompt
  ListFileNames,FilePath,File Path Name
  ListFileNames,FileNamesFileName,Enter the File Names File Name
  Danny,....,....
  ...,.,..
  ..,.,.

OK, it's quite different ...
A quick and dirty solution:

awk -v OFS=, 'BEGIN {
  print "Identifier", "Name", "Prompt"
  }
/BEGIN KGO/,/END KGO/ {
  if (/Identifier/ && !c++ && match($0, /"[^"]+"/))
    id = substr($0, RSTART + 1, RLENGTH - 2)
  if (/END KGO/) {
   for (j = 0; ++j <= i;)
     print id, d[j, "Name"], d[j, "Prompt"]
  c = i = x; split(x, d)    
    }
  }
/BEGIN DSSUBRECORD/,/END DSSUBRECORD/ {
    /BEGIN DSSUBRECORD/ && i++
    if (/Name|Prompt/ && match($0, /"[^"]+"/))
      d[i, $1] = substr($0, RSTART + 1, RLENGTH - 2)
      }' infile
$awk -F"\"" 'BEGIN{n_id=o_id=0;i=1;print "Id,Name,Prompt"}($1 ~ /Id/){f=1;n_id=$2;printf "%s,",$2}
($1 ~ /Name|Prompt/){if((f==0||f==2)&&(n_id==o_id)){if(f==2){f=0;printf n_id","$2","}else{print $2;f=2}};
if(f==1&&n_id!=o_id){if(i==1){printf "%s,",$2}else{printf "%s",$2};i++};if(i==3){printf "\n";o_id=n_id;i=1;f=2}}' inputfile

output is

Id,Name,Prompt
003,DIR_PATH,DIR_PATH
003,DB_SID,ENTER DB SID NAME
077,FILE,Enter File