Extract table from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract table from file
# 8  
Old 09-14-2015
How do you tell 2+ spaces from 2+ spaces for a missing field?
# 9  
Old 09-14-2015
Quote:
Originally Posted by RudiC
How do you tell 2+ spaces from 2+ spaces for a missing field?
No way to tell unless the FIELDWIDTHS of the Header
# 10  
Old 09-15-2015
Assuming that none of your headings are substrings of any earlier (from left to right) headings, you could try something like:
Code:
awk -F'  +' -v LM="$(getconf LINE_MAX)" '
t == 0 && NF > 1 {
	# 1st line of table found...
	for(i = 1; i <= NF; i++) {
		h[i] = $i
		f[i] = index($0, $i)
		l[i - 1] = f[i] - f[i - 1]
	}
	l[n = NF] = LM - f[NF]
	if(debug) {
		printf("$0=\"%s\"\n", $0)
		for(i=1;i<=NF;i++)
			printf("h[%d]=%s,f[]=%d,l[]=%d\n",
				i,h[i],  f[i],  l[i])
	}
	t = 1
	next
}
t == 1 {# 2nd line of table found...
	if(debug)
		printf("$0=\"%s\"\n", $0)
	for(i = 1; i <= n; i++) {
		d = substr($0, f[i], l[i])
		gsub(/^ *| *$/, "", d)
		printf("%s=%s%s", h[i], d, i == n ? "\n" : ",")
	}
	t = 0
	next
}
1	# print non-table lines...
' file

which, with you sample input file, produces the 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..

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 09-15-2015
Thank you, It works!
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