943,847 Members | Top Members by Rank

Ad:
Jun 6th, 2007
0

Best way to skip first line?

Expand 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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rusman is offline Offline
17 posts
since Feb 2006
Jun 7th, 2007
0

Re: Best way to skip first line?

more +2 <file>
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jun 14th, 2007
0

Re: Best way to skip first line?

Or use sed
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 19th, 2007
0

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vrgurav is offline Offline
11 posts
since Jun 2007
Jun 19th, 2007
0

Re: Best way to skip first line?

Click to Expand / Collapse  Quote originally posted by rusman ...
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vrgurav is offline Offline
11 posts
since Jun 2007
Jun 20th, 2007
0

Re: Best way to skip first line?

Thanks all, I used more as that was the first response and it worked!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rusman is offline Offline
17 posts
since Feb 2006
Dec 10th, 2008
0

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.

Shell Scripting Syntax (Toggle Plain Text)
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4.  
  5. #define BUFFER_SIZE 4096
  6.  
  7. int main() {
  8. // Use block I/O
  9. char buff[BUFFER_SIZE];
  10. int firstLine = 1;
  11. size_t numRead;
  12. while((numRead = read(STDIN_FILENO, buff, BUFFER_SIZE))!=0) {
  13. if(numRead==-1) {
  14. perror("read");
  15. return 1;
  16. }
  17. size_t writePos = 0;
  18. // Skip first line
  19. if(firstLine) {
  20. while(writePos<numRead) {
  21. if(buff[writePos++]=='\n') {
  22. firstLine = 0;
  23. break;
  24. }
  25. }
  26. }
  27. while(writePos<numRead) {
  28. size_t numWritten = write(STDOUT_FILENO, buff+writePos, numRead-writePos);
  29. if(numWritten==-1) {
  30. perror("write");
  31. return 1;
  32. }
  33. writePos += numWritten;
  34. }
  35. }
  36. return 0;
  37. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dan_armstrong is offline Offline
1 posts
since Dec 2008
Dec 10th, 2008
0

Re: Best way to skip first line?

Hey there,

Did I miss:

Quote ...
sed 1d FILENAME
? Just checking

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: resetting an array
Next Thread in Shell Scripting Forum Timeline: integer division! oh the fun :-)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC