Extract table from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract table from file
# 1  
Old 09-14-2015
Extract table from file

I need to sort out table in a file to a format below:


Input:

Code:
this is a test
example
Cat   Bee     Dat
1     2       3
more Example
date
data
Bet  Cla   Blaa        Dat
A          6           T
data..


Output:

Code:
this is a test
example
Cat=1,Bee=2,Dat=3
more Example
date
data
Bet=A,Cla=,Blaa=6,Dat=T
data..

# 2  
Old 09-14-2015
Help us help you: how can the "table entries" be told from the rest of the text? Separator? Line number?
# 3  
Old 09-14-2015
Quote:
Originally Posted by RudiC
Help us help you: how can the "table entries" be told from the rest of the text? Separator? Line number?
table are usually delimited by 2 or more spaces.
# 4  
Old 09-14-2015
And there's NO other occurrence of 2+ spaces in other lines? Tables are always 2 lines?

---------- Post updated at 11:55 ---------- Previous update was at 11:54 ----------

How to identify the missing field?
# 5  
Old 09-14-2015
Quote:
Originally Posted by RudiC
And there's NO other occurrence of 2+ spaces in other lines? Tables are always 2 lines?
Only Tables have 2+ spaces and always 2 lines (Header and data below)
# 6  
Old 09-14-2015
Try
Code:
awk -F"  +" '
NF > 1  {if (++CNT%2)   {split ($0, T)
                         next 
                        }
         else            for (i=1; i<=NF; i++) $i=T[i] "=" $i
        }
1
' OFS="," file
this is a test
example
Cat=1,Bee=2,Dat=3
more Example
date
data
Bet=A,Cla=6,Blaa=T
data..

The "missing field" question is still open...
# 7  
Old 09-14-2015
Quote:
Originally Posted by RudiC
Try
Code:
awk -F"  +" '
NF > 1  {if (++CNT%2)   {split ($0, T)
                         next 
                        }
         else            for (i=1; i<=NF; i++) $i=T[i] "=" $i
        }
1
' OFS="," file
this is a test
example
Cat=1,Bee=2,Dat=3
more Example
date
data
Bet=A,Cla=6,Blaa=T
data..

The "missing field" question is still open...
Thank you for your effort, data is missing for a field if nothing is below it. e.g. data is missing for Cla as seen below:

Code:
Bet  Cla   Blaa        Dat
A          6           T

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parameterizing to dynamically generate the extract file from Oracle table using Shell Script

I have below 2 requirements for parameterize the generate the extract file from Oracle table using Shell Script. Could you please help me by modifying the script and show me how to execute it. First Requirement: I have a requirement where I need to parameterize to generate one... (0 Replies)
Discussion started by: hareshvikram
0 Replies

2. Shell Programming and Scripting

Extract hive table structure

Hi, I need to extract only the create table structure with columns alone. for eg hive_table show create table hive_table: create table hive_table(id number,age number) OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs:/path/' I need only below ... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

3. Shell Programming and Scripting

Oracle table extract: all columns are not converting into pipe delimited in flat file

Hi All, I am writing a shell script to extract oracle table into a pipe dilemited flat file. Below is my code and I have attached two files that I have abled to generate so far. 1. Table.txt ==> database extract file 2. flat.txt ==> pipe delimited after some manipulation of the original db... (5 Replies)
Discussion started by: express14
5 Replies

4. UNIX for Dummies Questions & Answers

Extract table from an HTML file

I want to extract a table from an HTML file. the table starts with <table class="tableinfo" and ends with next closing table tag </table> how can I do this with awk/sed... ---------- Post updated at 04:34 PM ---------- Previous update was at 04:28 PM ---------- also I want to... (4 Replies)
Discussion started by: koutroul
4 Replies

5. Shell Programming and Scripting

connecting to table to extract multiple rows into file from unix script

I need to extract the data from oracle table and written the below code. But it is not working.There is some problem with the query and output is shown is No rows selected" . If I run the same query from sql developer there is my required output. And if I run the shell script with simple sql... (7 Replies)
Discussion started by: giridhar276
7 Replies

6. Shell Programming and Scripting

Extract Mysql table output to a log file

I need to compare the 2 mysql database tables. there are around 50 tables in each DB. my idea is in DB1 extract result select * from table1; to alog file1 in DB2 extract result select * from table1; to alog file2 now compare log file 1 file 2 pls help me out ... thanks in advance (5 Replies)
Discussion started by: kalyankalyan
5 Replies

7. Shell Programming and Scripting

Extract table name from DDL

How can I extract table name from the different DDL statement like ALTER TABLE CREATE TABLE etc Basically I have to parse thr the any of the DDL statement and verify if that DDL statement is implemented by DBA or not. how can i do this efficiently in Kornshell scripting. (2 Replies)
Discussion started by: gayathree
2 Replies

8. Shell Programming and Scripting

Shell script to extract rows from table

I have an Employee with EID, ENAME and ESTATUS as columns in SQL. I want to extract the status of an employee and update the details if the status is 'A'. Can anyone help in writing the shell script. (1 Reply)
Discussion started by: vkca
1 Replies

9. UNIX for Dummies Questions & Answers

extract table name from a control file

Hi, I need to extract the table name from an oracle control file which comes as the last word in the third line. Ex: LOAD DATA INFILE '/home/user/files/scott.dat' INTO TABLE SCOTT.EMP_SAL FIELDS TERMINATED BY.......... what i want to to is write the table name SCOTT.EMP_SAL to a... (2 Replies)
Discussion started by: mwrg
2 Replies

10. Shell Programming and Scripting

Extract Table from PDF

Hi Guys! I want to extract table from PDF in HTML. Can we do this using Shell script....??. Please provide me your suggestions. Any help will be highly appreciated. Thanks! (2 Replies)
Discussion started by: parshant_bvcoe
2 Replies
Login or Register to Ask a Question