RSS Forums RSS

Best way to skip first line?

Please support our Shell Scripting advertiser: Programming Forums
Reply
Posts: 16
Reputation: rusman is an unknown quantity at this point 
Solved Threads: 0
rusman rusman is offline Offline
Newbie Poster

Question Best way to skip first line?

  #1  
Jun 6th, 2007
I need to read in a list from a file, but I want to always ignore/skip the first line of this file.

Right now I'm just cat'ing the file and awk'ing the first column as that's all I need. However, this will also give me the first column of the first line which I do not want.

The first entry will probably be the same value everytime, so I could put a test in to skip that, but wondered if there was another quicker way and so that I'm not always testing every value.

Thanks!
AddThis Social Bookmark Button
Reply With Quote  
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 26
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Best way to skip first line?

  #2  
Jun 7th, 2007
more +2 <file>
Reply With Quote  
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Best way to skip first line?

  #3  
Jun 14th, 2007
Or use sed
Are you Agile.. ?
Reply With Quote  
Posts: 11
Reputation: vrgurav is an unknown quantity at this point 
Solved Threads: 1
vrgurav vrgurav is offline Offline
Newbie Poster

Re: Best way to skip first line?

  #4  
Jun 19th, 2007
tail +n <filename>

to start from second line
eg : tail +2 <filename>

Hope this will solve your query
Reply With Quote  
Posts: 11
Reputation: vrgurav is an unknown quantity at this point 
Solved Threads: 1
vrgurav vrgurav is offline Offline
Newbie Poster

Re: Best way to skip first line?

  #5  
Jun 19th, 2007
Originally Posted by rusman View Post
I need to read in a list from a file, but I want to always ignore/skip the first line of this file.

Right now I'm just cat'ing the file and awk'ing the first column as that's all I need. However, this will also give me the first column of the first line which I do not want.

The first entry will probably be the same value everytime, so I could put a test in to skip that, but wondered if there was another quicker way and so that I'm not always testing every value.

Thanks!
tail +n filename

eg: tail +2 filename
Reply With Quote  
Posts: 16
Reputation: rusman is an unknown quantity at this point 
Solved Threads: 0
rusman rusman is offline Offline
Newbie Poster

Re: Best way to skip first line?

  #6  
Jun 20th, 2007
Thanks all, I used more as that was the first response and it worked!!
Reply With Quote  
Posts: 1
Reputation: dan_armstrong is an unknown quantity at this point 
Solved Threads: 0
dan_armstrong dan_armstrong is offline Offline
Newbie Poster

Re: Best way to skip first line?

  #7  
Dec 10th, 2008
Here is what I did as the "fastest" way to skip the first line. Fastest=best for my situation.

#include <unistd.h>
#include <stdio.h>
#include <errno.h>

#define BUFFER_SIZE 4096

int main() {
  // Use block I/O
  char buff[BUFFER_SIZE];
  int firstLine = 1;
  size_t numRead;
  while((numRead = read(STDIN_FILENO, buff, BUFFER_SIZE))!=0) {
    if(numRead==-1) {
      perror("read");
      return 1;
    }
    size_t writePos = 0;
    // Skip first line
    if(firstLine) {
      while(writePos<numRead) {
        if(buff[writePos++]=='\n') {
          firstLine = 0;
          break;
        }
      }
    }
    while(writePos<numRead) {
      size_t numWritten = write(STDOUT_FILENO, buff+writePos, numRead-writePos);
      if(numWritten==-1) {
        perror("write");
        return 1;
      }
      writePos += numWritten;
    }
  }
  return 0;
}
Reply With Quote  
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Best way to skip first line?

  #8  
Dec 10th, 2008
Hey there,

Did I miss:

sed 1d FILENAME

? Just checking

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 8526 | Replies: 7 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:33 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC