Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

how to remove the first line from a flat file ?

UNIX for Dummies Questions & Answers


Tags
sed delete line, shell script, shell scripting, unix commands, unix scripting, unix scripting basics

 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-17-2005
xli xli is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 14 June 2005, 7:57 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
how to remove the first line from a flat file ?

Hi,

I want to remove the first line from a flat file using unix command as simple as possible. Can anybody give me a hand ?

Thanks in advance.

xli
Sponsored Links
    #2  
Old Unix and Linux 03-17-2005
reborg's Unix or Linux Image
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 13 Times in 11 Posts
printf ":1d\n:wq\n" | ex name_of_your_flatfile
Sponsored Links
    #3  
Old Unix and Linux 03-17-2005
tanku tanku is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 31 March 2005, 1:56 PM EST
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
another way,

sed -i '1d' filename
    #4  
Old Unix and Linux 03-17-2005
reborg's Unix or Linux Image
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 13 Times in 11 Posts
That does not remove the line from the file, is streams the file to stdout with the first line removed. It would need to be redirected into a temporary file and moved back to replace the original file.

Also if I am correct, and I remember correctly the flatfile xli is talking about is pretty huge (millions of lines), and do it that way could be pretty slow.
Sponsored Links
    #5  
Old Unix and Linux 03-17-2005
tanku tanku is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 31 March 2005, 1:56 PM EST
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for the explanation. i give another reply

perl -pi -e '$_ = "" if ($. == 1);' filename

which one is fastest?
Sponsored Links
    #6  
Old Unix and Linux 03-17-2005
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 25 Times in 23 Posts
Quote:
Originally Posted by reborg
That does not remove the line from the file, is streams the file to stdout with the first line removed. It would need to be redirected into a temporary file and moved back to replace the original file.
Not always - it depends on which version of sed you're using.... GNU sed supports the -i option to edit files in place (a backup extension can also be supplied much like perls -i option).

Observe...

Code:
$ cat > flatfile
1st line
2nd line
3rd line
^D
$ sed -i '1d' flatfile
$ more flatfile
2nd line
3rd line

Cheers
ZB
Sponsored Links
    #7  
Old Unix and Linux 03-17-2005
xli xli is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 14 June 2005, 7:57 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
perl -pi -e '$_ = "" if ($. == 1);' filename

works very well.

Thanks very much for your help, Tanku and Reborg.

Regards, xli
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove numeric characters in the flat file rafimd1985 UNIX for Dummies Questions & Answers 4 03-04-2012 12:02 AM
Remove the footer from a flat file by searching a pattern gani_85 Shell Programming and Scripting 6 10-03-2011 03:05 AM
Remove somewhat Duplicate records from a flat file jolney Shell Programming and Scripting 4 09-29-2011 05:01 PM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 09:50 PM
How to remove page breaks from a flat file??? kumarsaravana_s Shell Programming and Scripting 5 04-17-2007 09:51 AM



All times are GMT -4. The time now is 06:11 AM.