Skip to Main Content
  • Questions
  • How to skip header and footer line a file while accessing records from external table?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Ganesh.

Asked: November 22, 2016 - 9:21 am UTC

Last updated: November 11, 2022 - 3:30 am UTC

Version: 10g

Viewed 10K+ times! This question is

You Asked

Hi Tom,

You guys are doing a great job.

How to skip header and footer like (first and last line) in a .dat file while loading records from external table concept.
I can able to remove those line in Unix but they are having information about record count for a particular table andso i don't wwant to remove those lines but needs to skip those line.So while accessing those files from external tables i am getting error.So

Please give me a solution to call that file by skipping header and footer via external table.I have searched so many things in internet but still struggling to find the solution.Please help me to resolve this issue.

Thanks in advance.


and Chris said...

You can bypass header rows with the skip access parameter. I'm not aware of a way to specify a number of footer rows to avoid.

But if they're in a standard format you can stop Oracle loading them with the "load when" clause. Use this to load rows that are not like the footer.

For example, if you have this file:

skip this
real data
and skip this


Line 2 is the only one you want to load. Use "skip 1" to jump to the second line. And "load when (text != 'and skip this')" to prevent the final line loading:

create table t (
  text varchar2(1000)
) organization external (
  default directory tmp
  access parameters (
   records delimited by newline
   load when (text != 'and skip this')
   skip 1
  )
  location ('test.txt')
);

select * from t;

TEXT       
real data

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Adding a little flexibility

Dexter Jones, November 09, 2022 - 4:34 pm UTC

I agree - keep up the great work, guys.

If your file has a footer that is not static, perhaps with a line count, you will need a bit more flexibility to deal with it. In my case, the footer is: LINE COUNT: nnn where nnn varies with each file.

The solution in this case is qualify the position of the constant portion of the footer text to load when .

In my case it is always at the beginning of the line which simplifies things somewhat.

create table t (
text varchar2(1000)
) organization external (
default directory tmp
access parameters (
records delimited by newline
-- load when (text != 'and skip this') -- good for static text.
load when (1:10) != 'LINE COUNT' -- handles changing values.
skip 1
)
location ('test.txt')
);


PS: This isn't in the Database Utilites doc (for 19c), but it might be nice to add an example for future reference. :)
Connor McDonald
November 11, 2022 - 3:30 am UTC

good stuff