DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   Best way to skip first line? (http://www.daniweb.com/forums/thread80245.html)

rusman Jun 6th, 2007 4:22 pm
Best way to skip first line?
 
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!

ghostdog74 Jun 7th, 2007 10:48 am
Re: Best way to skip first line?
 
more +2 <file>

thekashyap Jun 14th, 2007 5:52 am
Re: Best way to skip first line?
 
Or use sed

vrgurav Jun 19th, 2007 8:20 am
Re: Best way to skip first line?
 
tail +n <filename>

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

Hope this will solve your query

vrgurav Jun 19th, 2007 8:22 am
Re: Best way to skip first line?
 
Quote:

Originally Posted by rusman (Post 382326)
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

rusman Jun 20th, 2007 6:37 pm
Re: Best way to skip first line?
 
Thanks all, I used more as that was the first response and it worked!!

dan_armstrong Dec 10th, 2008 8:47 pm
Re: Best way to skip first line?
 
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;
}

eggi Dec 10th, 2008 10:43 pm
Re: Best way to skip first line?
 
Hey there,

Did I miss:

Quote:

sed 1d FILENAME
? Just checking :)

, Mike


All times are GMT -4. The time now is 9:13 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC