1. 首页 > 科技

通过shell截图txt文件中特定的范围的字符串?(如何用shell提取文件中指定的字符串)

通过shell截图txt文件中特定的范围的字符串?(如何用shell提取文件中指定的字符串)

如何用shell提取文件中指定的字符串

代码如下:

#include "stdio.h"//

#include "string.h"//

#include "stdlib.h"//

void main(void){

FILE *pR,*pW;

char CoStr[20],ln,ch,i=0;

if(!(pR=fopen("ABC.txt","r"))){

printf("源文件打开失败...");

exit(0);

}

printf("请输入特定字符串...\nCoStr=");

ln=strlen(gets(CoStr));

do{

fseek(pR,-i,SEEK_CUR);

for(i=0;i<ln;i++)

if((ch=getc(pR))!=CoStr[i]) break;

if(ch==EOF){

printf("没有发现特定字符串%s!\n",CoStr);

exit(0);

}

}while(i<ln);

if(!(pW=fopen("XYZ.txt","w"))){

printf("创建目标文件失败...");

exit(0);

}

while((ch=getc(pR))!='"' && ch!=(char)176);

while((ch=getc(pR))!='"' && ch!=(char)177){

if(ch=='\n' || ch==' ') continue;

putc(ch,pW);

}

fclose(pW);

fclose(pR);

printf("文件已成功建立,名为XYZ.txt\n");

}

Linux下shell截取指定本文内的字符串

sqlldr | sed -n '/CQCS_OPT.I_P_GU_PLED_INFO:$/{h;:a;n;/^$/!{H;ba};g;p}'这样实现跟行数无关。

要将结果导入到另一个文件里,用输出重定向,命令后面加 >>output

如何用shell提取文件中指定的字符串

最简单的方法就是用grep

cat filename | grep "字符串"

使用shell根据条件提取文件中指定的字符串

root@localhost:~/xly/02# cat a

013.000.000 XXXxx Wwww [02111]

root@localhost:~/xly/02# cat b

02111

root@localhost:~/xly/02# cat abc.sh

#!/bin/bash

for i in `cat b`

do

awk '/'$i'/{print $2," ",$3}' a >>c

done

cat c

确定需要的内容是第二列和第三列哦~

root@localhost:~/xly/02# cat c

XXXxx Wwww