代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
文本中搜索匹配行
作者:
DDT
/ 发布于
2012/7/16
/
648
文本中搜索匹配行
<div>/* findstr.c */ /* Copyright (C) 1994-2002, Jim Hall <jhall@freedos.org> */ /* Adapted for ReactOS -Edited for Findstr.exe K'Williams */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* This program locates a string in a text file and prints those lines * that contain the string. Multiple files are clearly separated. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <windows.h> #include <io.h> #include <dos.h> /* Symbol definition */ #define MAX_STR 1024 /* This function prints out all lines containing a substring. There are some * conditions that may be passed to the function. * * RETURN: If the string was found at least once, returns 1. * If the string was not found at all, returns 0. */ int find_str (char *sz, FILE *p, int invert_search, int count_lines, int number_output, int ignore_case, int at_start, int literal_search, int at_end, int reg_express, int exact_match, int sub_dirs, int only_fname) { int i, length; long line_number = 0, total_lines = 0; char *c, temp_str[MAX_STR], this_line[MAX_STR]; /* Convert to upper if needed */ if (ignore_case) { length = strlen (sz); for (i = 0; i < length; i++) sz[i] = toupper (sz[i]); } /* Scan the file until EOF */ while (fgets (temp_str, MAX_STR, p) != NULL) { /* Remove the trailing newline */ length = strlen (temp_str); if (temp_str[length-1] == '\n') { temp_str[length-1] = '\0'; } /* Increment number of lines */ line_number++; strcpy (this_line, temp_str); /* Convert to upper if needed */ if (ignore_case) { for (i = 0; i < length; i++) temp_str[i] = toupper (temp_str[i]); } /* Locate the substring */ /* strstr() returns a pointer to the first occurrence in the string of the substring */ c = strstr (temp_str, sz); if ( ((invert_search) ? (c == NULL) : (c != NULL)) ) { if (!count_lines) { if (number_output) printf ("%ld:", line_number); /* Print the line of text */ puts (this_line); } total_lines++; } /* long if */ } /* while fgets */ if (count_lines) { /* Just show num. lines that contain the string */ printf ("%ld\n", total_lines); } /* RETURN: If the string was found at least once, returns 1. * If the string was not found at all, returns 0. */ return (total_lines > 0 ? 1 : 0); } /* Main program */ void main (int argc, char **argv) { char *opt, *needle = NULL; int ret = 0; int invert_search = 0; /* flag to invert the search */ int count_lines = 0; /* flag to whether/not count lines */ int number_output = 0; /* flag to print line numbers */ int ignore_case = 0; /* flag to be case insensitive */ int at_start = 0; /* flag to Match if at the beginning of a line. */ int at_end = 0; /* flag to Match if at the beginning of a line. */ int reg_express = 0; /* flag to use/not use regular expressions */ int exact_match = 0; /* flag to be exact match */ int sub_dirs= 0; /* this and all subdirectories */ int only_fname= 0; /* print only the name of the file*/ int literal_search=0; FILE *pfile; /* file pointer */ int hfind; /* search handle */ struct _finddata_t finddata; /* _findfirst, filenext block */ /* Scan the command line */ while ((--argc) && (needle == NULL)) { if (*(opt = *++argv) == '/') { switch (opt[1]) { case 'b': case 'B': /* Matches pattern if at the beginning of a line */ at_start = 1; break; //case 'c': //case 'C': /* Literal? */ // literal_search = 1; // break; case 'e': case 'E': /* matches pattern if at end of line */ at_end = 1; break; case 'i': case 'I': /* Ignore */ ignore_case = 1; break; case 'm': case 'M': /* only filename */ only_fname = 1; break; case 'n': case 'N': /* Number */ number_output = 1; break; case 'r': case 'R': /* search strings as regular expressions */ reg_express = 1; break; case 's': case 'S': /* search files in child directory too*/ sub_dirs = 1; break; case 'v': case 'V': /* Not with */ invert_search = 1; break; case 'x': case 'X': /* exact match */ exact_match = 1; break; default: exit (2); /* syntax error .. return error 2 */ break; } } else { /* Get the string */ if (needle == NULL) { /* Assign the string to find */ needle = *argv; } } } /* Check for search string */ if (needle == NULL) { /* No string? */ exit (1); } /* Scan the files for the string */ if (argc == 0) { ret = find_str (needle, stdin, invert_search, count_lines, number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match, sub_dirs, only_fname); } while (--argc >= 0) { hfind = _findfirst (*++argv, &finddata); if (hfind < 0) { /* We were not able to find a file. Display a message and set the exit status. */ fprintf (stderr, "No such file!", *argv);// } else { /* repeat find next file to match the filemask */ do { /* We have found a file, so try to open it */ if ((pfile = fopen (finddata.name, "r")) != NULL) { printf ("---------------- %s\n", finddata.name); ret = find_str (needle, pfile, invert_search, count_lines, number_output, ignore_case, at_start, literal_search, at_end, reg_express, exact_match, sub_dirs, only_fname); fclose (pfile); } else { fprintf (stderr, "Cannot open the file %s!", finddata.name); } } while (_findnext(hfind, &finddata) > 0); } _findclose(hfind); } /* for each argv */ /* RETURN: If the string was found at least once, returns 0. * If the string was not found at all, returns 1. * (Note that find_str.c returns the exact opposite values.) */ exit ( (ret ? 0 : 1) ); }
试试其它关键字
索匹配行
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3