printcols.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*(c) Copyright Barry Kauler 2009*/
  2. /*Invoke like this: printcols afilename 1 6 2 9
  3. ...columns with '|' delimiter character get printed, in order specified.
  4. handles up to 15 columns, max line length 1023 characters.
  5. Designed for use in the Puppy Package Manager, puppylinux.com
  6. Compile statically:
  7. # diet gcc -nostdinc printcols.c -o printcols
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. int main (int argc, char ** argv) {
  12. int cntargs;
  13. char buffer1[2048];
  14. FILE* fp;
  15. char* ptoken1;
  16. char* ptoken2;
  17. char* ptoken3;
  18. char* ptoken4;
  19. char* ptoken5;
  20. char* ptoken6;
  21. char* ptoken7;
  22. char* ptoken8;
  23. char* ptoken9;
  24. char* ptoken10;
  25. char* ptoken11;
  26. char* ptoken12;
  27. char* ptoken13;
  28. char* ptoken14;
  29. char* ptoken15;
  30. int i;
  31. int cnt;
  32. int nextarg;
  33. fp = fopen(argv[1],"r");
  34. while( fgets(buffer1,2047,fp) != NULL ) {
  35. cnt=1;
  36. cntargs=2;
  37. ptoken1=&buffer1[0];
  38. for (i=0;i<2048;i=i+1) {
  39. if (buffer1[i]==0) break;
  40. if (buffer1[i]=='|') {
  41. cnt=cnt+1; buffer1[i]=0;
  42. if (cnt==2) ptoken2=&buffer1[i+1];
  43. else if (cnt==3) ptoken3=&buffer1[i+1];
  44. else if (cnt==4) ptoken4=&buffer1[i+1];
  45. else if (cnt==5) ptoken5=&buffer1[i+1];
  46. else if (cnt==6) ptoken6=&buffer1[i+1];
  47. else if (cnt==7) ptoken7=&buffer1[i+1];
  48. else if (cnt==8) ptoken8=&buffer1[i+1];
  49. else if (cnt==9) ptoken9=&buffer1[i+1];
  50. else if (cnt==10) ptoken10=&buffer1[i+1];
  51. else if (cnt==11) ptoken11=&buffer1[i+1];
  52. else if (cnt==12) ptoken12=&buffer1[i+1];
  53. else if (cnt==13) ptoken13=&buffer1[i+1];
  54. else if (cnt==14) ptoken14=&buffer1[i+1];
  55. else if (cnt==15) ptoken15=&buffer1[i+1];
  56. }
  57. }
  58. /*print the fields in requested order...*/
  59. for (cntargs=2;cntargs<argc;cntargs++) {
  60. nextarg=atoi(argv[cntargs]);
  61. if ( nextarg >= cnt ) { printf("|"); continue; } /*in case of lines with less fields*/
  62. if (nextarg==1) printf("%s|",ptoken1);
  63. else if (nextarg==2) printf("%s|",ptoken2);
  64. else if (nextarg==3) printf("%s|",ptoken3);
  65. else if (nextarg==4) printf("%s|",ptoken4);
  66. else if (nextarg==5) printf("%s|",ptoken5);
  67. else if (nextarg==6) printf("%s|",ptoken6);
  68. else if (nextarg==7) printf("%s|",ptoken7);
  69. else if (nextarg==8) printf("%s|",ptoken8);
  70. else if (nextarg==9) printf("%s|",ptoken9);
  71. else if (nextarg==10) printf("%s|",ptoken10);
  72. else if (nextarg==11) printf("%s|",ptoken11);
  73. else if (nextarg==12) printf("%s|",ptoken12);
  74. else if (nextarg==13) printf("%s|",ptoken13);
  75. else if (nextarg==14) printf("%s|",ptoken14);
  76. else if (nextarg==15) printf("%s|",ptoken15);
  77. }
  78. printf("\n");
  79. }
  80. fclose(fp);
  81. return 0;
  82. }