|
发表于 2004-11-15 20:04:51
|
显示全部楼层
编写C文件: getline.c
/*
* this is a file that can read a line from a file
*/
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAX_CHARS 1024
char usage[]="Usage: line [-n <Line number>] <filename>\n -h: help\n";
static int isnumber(const char*);
int main(int argc, char* argv[])
{
int line = 1;
char filename[MAX_CHARS] = {0};
char buffer[MAX_CHARS];
int fd;
int i;
int status;
struct stat st;
if ( argc < 2 ) {
printf(usage);
return 1;
}
if (strcmp(argv[1], "-n") == 0) {
if (isnumber(argv[2]))
line = atoi(argv[2]);
else {
printf(usage);
return 1;
}
strcpy(filename, argv[3]);
}
else
strcpy(filename, argv[1]);
if (stat(filename, &st)) {
perror("Error: ");
return 1;
}
if (!S_ISREG(st.st_mode)) {
printf("Error: must need regular file\n");
return 1;
}
fd = fopen(filename, "r");
for (i = 0; i < line; ++i) {
if (!fgets(buffer, MAX_CHARS, fd)) {
printf("Error: out of line ranger");
return 1;
}
}
printf(buffer);
return 0;
}
int isnumber(const char* s)
{
int i;
for (i = 0; i < strlen(s); ++i)
if (!isdigit(s))
break;
if (i == strlen(s))
return 1;
else
return 0;
}
编译:
gcc -o getline getline.c
cp getline /usr/bin/
写提取字段的脚本:
echo "$(getline -n 行号 文件名 | cut -c起始列号-截止列号)"
代码写的不是很好,应该用getopt来提取命令行选项。 |
|