how to remove the first line from a flat file ? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-16-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 03-17-2005
reborg's Avatar
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 11 Times in 11 Posts
printf ":1d\n:wq\n" | ex name_of_your_flatfile
Sponsored Links
    #3  
Old 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 03-17-2005
reborg's Avatar
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 11 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 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 03-17-2005
zazzybob's Avatar
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 24 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 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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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-03-2012 11:02 PM
Remove the footer from a flat file by searching a pattern gani_85 Shell Programming and Scripting 6 10-03-2011 02:05 AM
Remove somewhat Duplicate records from a flat file jolney Shell Programming and Scripting 4 09-29-2011 04:01 PM
remove specific lines from flat file using perl meghana Shell Programming and Scripting 12 02-12-2008 08:50 PM
How to remove page breaks from a flat file??? kumarsaravana_s Shell Programming and Scripting 5 04-17-2007 08:51 AM



All times are GMT -4. The time now is 10:07 AM.