批处理之家's Archiver

netdzb 发表于 2019-7-11 12:14

求个shell的匹配命令

有个10000行的纯文本文件,每行的字符是0,1,2的其中之一。求命令匹配符合条件的打印出提示字符串,如果文本中包括3等字符,
打印出不符合的提示字符串。

Batcher 发表于 2019-7-11 15:14

请给一段测试文本及其希望得到的结果

netdzb 发表于 2019-7-11 15:43

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=221801&ptid=53313]2#[/url] [i]Batcher[/i] [/b]

自己用C语言写了一下,如果有不匹配显示error和不匹配的的行。如果全部匹配,显示good和总行数。不知道这个Shell改怎么写?
想学习一下。

cek
error.
hangshu:7

cek
good.
hanshu:11

netdzb 发表于 2019-7-11 15:44

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=221801&ptid=53313]2#[/url] [i]Batcher[/i] [/b]

示范文本

0
1
2
1
0
1
2
0
1
1
2

--------------------------------

0
1
2
1
5
1
2
0
1
1
2

Batcher 发表于 2019-7-11 17:13

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=221804&ptid=53313]4#[/url] [i]netdzb[/i] [/b]


    这个示范文本是根据什么规律得出error. hangshu:7

netdzb 发表于 2019-7-11 17:16

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=221807&ptid=53313]5#[/url] [i]Batcher[/i] [/b]

这里应该是5,因为我测试好把示范文本给删除了。
后来重新弄了一个就对不上了。

cfwyy77_bat 发表于 2019-7-11 17:23

建议楼主一开始就把需求说的明白,准确一些,有文本处理的最好有示例样本。[code] awk '{if ($0 !~/[0-2]/) n++}END{if (n>0)printf("error,错误行数%d\n",n);else printf("good,总行数%d\n",NR)}' 1.txt
[/code]

Batcher 发表于 2019-7-11 17:24

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=221809&ptid=53313]6#[/url] [i]netdzb[/i] [/b]


    把你写的C代码发出来看看。另外,如果有多个不匹配的行,应该怎么显示呢。

netdzb 发表于 2019-7-11 18:13

[i=s] 本帖最后由 netdzb 于 2019-7-11 18:17 编辑 [/i]

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=221811&ptid=53313]8#[/url] [i]Batcher[/i] [/b]

#include <stdio.h>
#include <string.h>

int main()
{
        char buffer[1024] = {0};
        int len = 0;
        int count = 0;
        int flag;     

        FILE *fp = fopen("cek.txt", "r");
        if(NULL == fp)
        {
                printf("failed to open cek.txt\n");
                return 1;
        }
        
        while(!feof(fp))
        {
        fgets(buffer, sizeof(buffer) - 1, fp);
        count += 1;
        if ( strnicmp(buffer,"0",1) ==0 || strnicmp(buffer,"1",1) ==0 || strnicmp(buffer,"2",1) ==0 )
           flag = 1;
        else
           {
              flag = 0;
              break;
           }      
        } // while
        if ( flag == 1 )
           printf("good.\n");
        else
           printf("error.\n");   
        fclose(fp);
        printf("hangshu:%d\n", count);
        return 0;
}

我就测试了几行,不知道对不对?

netdzb 发表于 2019-7-11 18:50

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=221810&ptid=53313]7#[/url] [i]cfwyy77_bat[/i] [/b]


'{if ($0 !~/[0-2]/)

看看我对正则理解的对吗?
   
$0         表示 第一个字符
!           表示 不是
~           这个没看懂
/[0-2]     这个表示0,1,2

Batcher 发表于 2019-7-11 19:55

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=221817&ptid=53313]10#[/url] [i]netdzb[/i] [/b]


    这需要看教程,而不是猜。

cfwyy77_bat 发表于 2019-7-12 08:10

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=221817&ptid=53313]10#[/url] [i]netdzb[/i] [/b]
如版主说的,要自己所有细节都理解,肯定要看教程。要在shell下处理文本,正则,awk,sed,grep这些跑不了的。建议楼主看看相关教程。

$0不是正则,是awk的当前一个记录,默认是当前的一行。
!~  表示   不匹配。

Batcher 发表于 2019-7-12 10:56

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=221816&ptid=53313]9#[/url] [i]netdzb[/i] [/b][code]awk '{if($0!~/[0-2]/)n=NR}END{if(n>0)printf("error.\nhangshu:%d\n",n);else printf("good.\nhangshu:%d\n",NR)}' 1.txt[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.