The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append Header and Trailer balzzz UNIX for Dummies Questions & Answers 2 01-06-2008 08:19 AM
Copy all the files with time stamp and remove header,trailer from file ksrams UNIX for Dummies Questions & Answers 35 07-30-2007 02:15 PM
Removing trailer from a flat file!!! kumarsaravana_s UNIX for Dummies Questions & Answers 12 06-24-2007 03:53 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
Remove header(first line) and trailer(last line) in ANY given file madhunk Shell Programming and Scripting 2 03-13-2006 03:36 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-02-2009
karthi_gana karthi_gana is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Removing Header & Trailer from a file

Hi All,

I am karthik. I am new to this forum. I have one requirement.

I have a file with header and footer.

Header may be like

HDR0001
or
FILE20090110

(Assume it is unknown so far, but i am sure there is a header in the file)

likewise file has the trailer too.

I just want to remove the header & footer and BCP data into a table.

Note: The file size would be 1.5 GB to 2.0 GB.

So i am looking for some good way to remove the header and trailer.

Inputs are welcome!
  #2 (permalink)  
Old 10-02-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
This should do the trick:

Code:
awk 'NR<3{s=$0;next}{print s;s=$0}' file > newfile
or:

Code:
sed '1d;$d' file > newfile
  #3 (permalink)  
Old 10-02-2009
karthi_gana karthi_gana is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Thanks Franklin!

I tested the second one. It works fine.

But i don't want to create another file. I tested the script as below

filename > filename. After executing the script, the <filename> becomes empty.

so isn't possible to remove the header & footer and store it in the same file itself?

Also can you tell me what is the difference between the first one and second one? i mean which will give give good performance?

Last edited by karthi_gana; 10-02-2009 at 12:30 PM..
  #4 (permalink)  
Old 10-02-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Code:
awk 'NR<3{s=$0;next}{print s;s=$0}' file > newfile
Explanation of awk code:

Code:
NR<3{s=$0;next}
If line number < 3 assign the current line to variable s and read the next line

Code:
{print s;s=$0}
Print the previous line and assign the current line to variable s

Explanation of sed code:

Code:
sed '1d;$d'
Delete the first (1d) and the last line ($d)
  #5 (permalink)  
Old 10-02-2009
thegeek thegeek is offline
Registered User
  
 

Join Date: Apr 2009
Location: /usr/bin/vim
Posts: 450
Answer for one of your question:

How to make the edit in the same file itself,
use -i switch in sed as

Code:
sed -i <REQUIRED-PART>
  #6 (permalink)  
Old 10-07-2009
karthi_gana karthi_gana is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Quote:
Originally Posted by thegeek View Post
Answer for one of your question:

How to make the edit in the same file itself,
use -i switch in sed as

Code:
sed -i <REQUIRED-PART>

sed: illegal option -- i
$

i got the above error message.

i am using ksh.

is there any other option to do this?
  #7 (permalink)  
Old 10-08-2009
binlib binlib is offline
Registered User
  
 

Join Date: Aug 2009
Location: New Jersey
Posts: 61
Check the -F and -L command line options of bcp.

Last edited by binlib; 10-08-2009 at 08:58 PM..
Reply

Bookmarks

Tags
remove header and trailer

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0