标题: [文本处理] 批处理/sed/gawk中如何删除文本中每行里指定字符后的内容? [打印本页]
作者: 飞龙project 时间: 2020-2-21 12:33 标题: 批处理/sed/gawk中如何删除文本中每行里指定字符后的内容?
本帖最后由 飞龙project 于 2020-2-21 13:03 编辑
下面是一个文件(节选),我想删除每行里以?开头的之后的内容- #EXTM3U
- #EXT-X-VERSION:3
- #EXT-X-TARGETDURATION:20
- #EXT-X-MEDIA-SEQUENCE:0
- #EXTINF:17.760000,
- http://website/hls/junior/3/1CCMN4CC4D-hls0.ts?auth_key=1582255823-0-0-431c012620e68d9
- #EXTINF:6.440000,
- http://website/hls/junior/3/1CCMN4CC4D-hls1.ts?auth_key=1582255823-0-0-e68d8911766cc47
- #EXTINF:10.000000,
- http://website/hls/junior/3/1CCMN4CC4D-hls2.ts?auth_key=1582255823-0-0-02a02b662ed0ca8
- ·········
- #EXTINF:6.040000,
- http://website/hls/junior/3/1CCMN4CC4D-hls156.ts?auth_key=1582255823-0-0-4143e53286841c
- #EXT-X-ENDLIST
复制代码
变为:- #EXTM3U
- #EXT-X-VERSION:3
- #EXT-X-TARGETDURATION:20
- #EXT-X-MEDIA-SEQUENCE:0
- #EXTINF:17.760000,
- 1CCMN4CC4D-hls0.ts
- #EXTINF:6.440000,
- 1CCMN4CC4D-hls1.ts
- #EXTINF:10.000000,
- 1CCMN4CC4D-hls2.ts
- ·········
- #EXTINF:6.040000,
- 1CCMN4CC4D-hls156.ts
- #EXT-X-ENDLIST
复制代码
其中保留以#开头的内容,谢谢了!
作者: zaqmlp 时间: 2020-2-21 13:54
- sed -i "s/\?.*$//" "xx.txt"
复制代码
作者: 窄口牛 时间: 2020-2-21 14:57
作者: Gin_Q 时间: 2020-2-21 18:02
不支持中文!!!有没有兴趣学下C!- //Dec-C++ 5.11
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- int max=10240;
-
- FILE *fp(char *p_n,const char *mode);
- char *analyze(char *a);
-
- int main(int argc,char *argv[] )
- {
- FILE *s_txt;
- FILE *res_txt;
- s_txt=(fp(argv[1],"r"));
- res_txt=(fp(argv[2],"w"));
- char arr[max]="0";
- char *p=arr;
- char temp[max]="0";
- int i;
- while (fgets(temp,max,s_txt)!=NULL)
- {
- p=analyze(temp);
- fprintf(res_txt,"%s",p);
- }
- fclose(s_txt);
- fclose(res_txt);
- return 0;
- }
- FILE *fp(char *p_n,const char *mode)
- {
- FILE *fp;
- if ((fp = fopen(p_n,mode)) == NULL)
- {
- printf("%s open fail!!",p_n);
- getchar();
- exit(0);
- }
- return fp;
- }
-
- char *analyze(char *a)
- {
- static char res[1024]="0";
- char *p=res;
- if (a[0]=='#')
- {
- strcpy(res,a);
- return p;
- }
- char del='/';
- int i,j=0;
- for (i=0;a[i]!='\0';i++)
- {
- if (a[i]==del) j=0;
- if (a[i]!='?' && a[i]!=del)
- {
- *(p+j)=a[i];
- j++;
- }
- else if (a[i]=='?')
- {
- *(p+j)='\n';
- *(p+j+1)='\0';
- return p;
- }
-
- }
- strcpy(res,a);
- return p;
-
- }
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |