1

The below input data contains table attribures with header information. The expected output is given below as well where the header and newline with intended text. This is expected as and when there is a change in the table name.

Input Data

Table,Host,Database,Schema,column
DEPT,My Host,My Database,Integration,Deptno
EMP,My Host,My Database,stage,emp_no
EMP,My Host,My Database,stage,emp_name

Expected Output:

+++ Database Column - begin ++
Table,Host,Database,Schema,column
DEPT,My Host,My Database,Integration,Deptno
+++ Database Column - end +++
+++ Database Column - begin ++
Table,Host,Database,Schema,column
EMP,My Host,My Database,stage,emp_no
EMP,My Host,My Database,stage,emp_name
+++ Database Column - end +++
6
  • 2
    I can't really see a logic in the transformation between input and output. Could you describe a bit about what's happening?
    – Kusalananda
    Apr 24 '19 at 9:07
  • Hi,Because of formating issue data not appeared as expected.Now I have updated body of the question.
    – vijay
    Apr 24 '19 at 19:46
  • It's unclear whether the data is actually double-spaced (ever second line empty). Indent the data by four spaces or add ``` on otherwise empty lines before and after.
    – Kusalananda
    Apr 24 '19 at 19:54
  • Actual input data doesnt have any empty lines. I have added empty lines other wise multiple rows appearing as single row in the body.
    – vijay
    Apr 25 '19 at 5:48
  • I fixed the formatting.
    – Kusalananda
    Apr 25 '19 at 5:52
1

Maybe something like:

awk -F, '
  BEGIN {
    table = 1#st field
    begin = "+++ Database Column - begin +++"
    end = "+++ Database Column - end +++"
  }
  NR == 1 {begin = begin "\n" $0; next}
  $table != last_table {
    if (NR > 2) print end
    print begin
    last_table = $table
  }
  {print}
  END{if (NR > 1) print end}'
0

Here's one way:

$ perl -ne 'BEGIN{
                $header="+++ Database Column - begin +++"; 
                $footer="+++ Database Column - end +++";
            }
            if($.==1){
                $header.="\n$_"; 
                next;
            } 
            print "$header$_$footer\n"' file 
+++ Database Column - begin +++
Name,Host,Database,Schema,Table,Description,ODBC Type,Data Type,Native Type
Deptno,My Host,My Database,Integration,Dept,Description of Column,INTEGER,INT16,Number
+++ Database Column - end +++
+++ Database Column - begin +++
Name,Host,Database,Schema,Table,Description,ODBC Type,Data Type,Native Type
empno,My Host,My Database,stage,emp,Description of Column,INTEGER,INT16,Number
+++ Database Column - end +++
+++ Database Column - begin +++
Name,Host,Database,Schema,Table,Description,ODBC Type,Data Type,Native Type
empname,My Host,stage,emp,Description of Column,VARCHAR,STRING,Text
+++ Database Column - end +++

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.