LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-27-2009, 03:34 PM   #1
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Rep: Reputation: 16
Question Copying All But the First Line into a New File


All:

I have a text file and I would like to copy all the lines except the first one to a new text file.

I know I could do this using sed/awk but I wasn't able to figure out, how exactly would I write that.

Looking forward to some help!
 
Old 03-27-2009, 04:43 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,009
Blog Entries: 5

Rep: Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787
sed 1,1d <filename>

The 1,1d tells sed to delete the lines in that range.
 
Old 03-27-2009, 04:46 PM   #3
number-g
LQ Newbie
 
Registered: Mar 2009
Distribution: Debian
Posts: 16

Rep: Reputation: 2
if you have tcl installed you could create this file:

Code:
#!/usr/bin/tclsh

set file [open /path/to/yourfile]
set data [read $file]
close $file

set data [split $data \n]
set data [lreplace $data 0 0]

set newfile [open /path/to/newfile w]
puts $newfile [join $data \n]
close $newfile
and make it executable.

hope this helps,
g

Last edited by number-g; 03-27-2009 at 04:50 PM. Reason: had included some locally specific stuff i was using to test the code
 
Old 03-28-2009, 12:57 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Code:
n=`wc -l file.ext|cut -d' ' -f1`
let n=$n-1
tail -n file.ext >newfile.txt
 
Old 03-28-2009, 02:57 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,314

Rep: Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032Reputation: 1032
Kiss - sed wins. Personally I would use the inverse "sed -n '2,$p' your-file > newfile"

Last edited by syg00; 03-28-2009 at 03:00 AM.
 
Old 03-29-2009, 08:39 AM   #6
senthilmuthiah
Member
 
Registered: Mar 2009
Distribution: Ubuntu, Fedora
Posts: 56

Original Poster
Rep: Reputation: 16
Talking

This is one of the many reasons why I love LinuxQuestions.org. It has high energy and we could get things done very fast.

Thank you all. All your solutions worked!
 
Old 03-22-2014, 11:39 AM   #7
Geremia
Member
 
Registered: Apr 2011
Distribution: Slackware 14.1
Posts: 213

Rep: Reputation: 5
Can't head or tail with a negative number of lines (negative "-n" flag) do this?
 
Old 03-22-2014, 12:58 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,211

Rep: Reputation: 572Reputation: 572Reputation: 572Reputation: 572Reputation: 572Reputation: 572
Quote:
Originally Posted by Geremia View Post
Can't head or tail with a negative number of lines (negative "-n" flag) do this?
Yes - "tail -n +2" to have it start output on the second line...
 
  


Reply

Tags
copy, file, lines, particular


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line progress bar for copying Maverick1182 Linux - Newbie 6 03-03-2012 12:55 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 11:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration