Best way to skip first line?
Please support our Shell Scripting advertiser: Programming Forums
![]() |
•
•
Posts: 16
Reputation:
Solved Threads: 0
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!
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!
•
•
Posts: 11
Reputation:
Solved Threads: 1
•
•
•
•
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!
eg: tail +2 filename
•
•
Posts: 1
Reputation:
Solved Threads: 0
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;
}•
•
Posts: 399
Reputation:
Solved Threads: 47
Hey there,
Did I miss:
? Just checking
, Mike
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Similar Threads
Other Threads in the Shell Scripting Forum
- Reading data from a textfiletype (C)
- Help with indenting infile -> outfile paragraph... (C++)
- How to skip a line ! (Perl)
- read each line of file (C)
- Reading specific line in a file. / Searching (C++)
- using ifstream, counting elements of each line in a dat file (C++)
- Plz, helping with writing a bit of code. Thanks (C++)
Other Threads in the Shell Scripting Forum
- Previous Thread: resetting an array
- Next Thread: integer division! oh the fun :-)
•
•
•
•
Views: 8526 | Replies: 7 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode