Appending column to rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending column to rows
# 1  
Old 07-17-2012
Appending column to rows

Hi All,
Input.txt
Code:
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:
Code:
Id,Name,Prompt 003,DIR_Path,Enter DIR_Path 007,File,Enter File

I have tried:
Code:
$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...
Code:
$grep -i -e Id -e Name -e Prompt Input.txt | sed '/   //g' > temp
$paste -sd',,\n' temp

# 2  
Old 07-17-2012
Code:
awk -v c="Id Name Prompt" -v OFS=, 'BEGIN {
  n = split(c, t)
  for (i = 0; ++i <= n;) {
    printf "%s", (t[i] (i < n ? OFS : ORS))
    v[t[i]]
    }
  }
$1 in v { get_val($1) }
/END RECGET/ {
  for (i = 0; ++i <= n;)
    printf "%s", (d[t[i]] (i < n ? OFS : ORS))
  }
func get_val(x) {
  match($0, /"[^"]+"/) &&
    r = substr($0, RSTART + 1, RLENGTH - 2)
  d[x] = r 
  }' infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 07-18-2012
Code:
$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

This User Gave Thanks to raj_saini20 For This Post:
# 4  
Old 07-19-2012
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
Code:
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:
Code:
Id,Name,Prompt
003,DIR_PATH,Enter DIR_PATH
003,DB_SID,ENTER DB SID NAME
077,FILE,Enter File

# 5  
Old 07-19-2012
This is what I get when I run my code with your sample input:

Code:
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] (i < n ? OFS : ORS))
    v[t[i]]
    }
  }
$1 in v { get_val($1) }
/END RECGET/ {
  for (i = 0; ++i <= n;)
    printf "%s", (d[t[i]] (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?
# 6  
Old 07-19-2012
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 ",,,,"

Code:
   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:
Code:
  Identifier,Name,Prompt
  ListFileNames,FilePath,File Path Name
  ListFileNames,FileNamesFileName,Enter the File Names File Name
  Danny,....,....
  ...,.,..
  ..,.,.

# 7  
Old 07-19-2012
OK, it's quite different ...
A quick and dirty solution:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

2. Shell Programming and Scripting

Appending = in particular column in csv file

Hi, I have a requirement to append = in particular row in csv file. Data in csv is as follow: row1,a,a,a row2,b,b,b row3,c,c,c row4,d,d,d csv should be modified at row3 and no. of columns are not fixed but rows are. output should be as: row1,a,a,a row2,b,b,b row3,=c,=c,=c... (2 Replies)
Discussion started by: Divya1987
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

5. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. UNIX for Dummies Questions & Answers

Converting column to rows for every 3 lines in the column

Hi gurus! Please help me with this one. I have an file with the following contents: a b c d e f g h i j I would like to make to transform it to look like this as my output file: a,b,c d,e,f (4 Replies)
Discussion started by: kokoro
4 Replies

7. Shell Programming and Scripting

Appending new column to existing files

Hi, i want to add another column to existing files containing strings and need to have the final output as a csv file. i have quite a number of files, each with varying number of rows and i need to append the string "test" for all the valid rows for each file. my sample raw files looks like this... (8 Replies)
Discussion started by: ida1215
8 Replies

8. UNIX for Dummies Questions & Answers

How to sum rows in e.g. column 1 by a category in e.g. column 2

Hi, I've shown an example of what I would like to achieve below. In the example file, I would like to sum the values in column 2 for each distinct category in column 3 (presumably making an array?) and print the sum as well as the category name and length (note:length always corresponds with... (8 Replies)
Discussion started by: auburn
8 Replies

9. Shell Programming and Scripting

appending column file

Hi all, I have two files with the same number of lines the first file is a.dat and looks like 0.000 1.000 1.000 2.000 ... the fields are tab separated the second file is b.dat and looks like 1.2347 0.546 2.3564 0.321 ... the fields are tab separated I would like to have a file c.dat... (4 Replies)
Discussion started by: f_o_555
4 Replies

10. Shell Programming and Scripting

Appending a column in one file to the corresponding line in a second

It appears that this has been asked and answered in similar fashions previously, but I am still unsure how to approach this. I have two files containing user information: fileA ttim:/home/ttim:Tiny Tim:632 ppinto:/home/ppinto:Pam Pinto:633 fileB ttim:xkfgjkd*&#^jhdfh... (3 Replies)
Discussion started by: suzannef
3 Replies
Login or Register to Ask a Question