Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Awk 如何根据模式的一部分用字符串替换模式?_Awk_Gawk - Fatal编程技术网

Awk 如何根据模式的一部分用字符串替换模式?

Awk 如何根据模式的一部分用字符串替换模式?,awk,gawk,Awk,Gawk,我有以下问题。我正在解释一个输入文件,现在我遇到了这样一个问题: 我需要将%%BLANKx转换为x空格 因此,在输入文件中,我发现例如%%BLANK8,我需要将%%BLANK8替换为8空格,%%BLANK10替换为10空格等。您可以在%%BLANK标记上拆分字符串。 之后,您可以读取任何令牌中的第一个数字,并将其转换为空格。 现在,您可以将每个令牌合并到一个新字符串中。试试这个。我还没有完全测试过 $ awk '/BLANK/{ match($0,/%%BLANK([0-9]+)/,a);s=s

我有以下问题。我正在解释一个输入文件,现在我遇到了这样一个问题: 我需要将
%%BLANKx
转换为
x
空格


因此,在输入文件中,我发现例如
%%BLANK8
,我需要将
%%BLANK8
替换为
8
空格,
%%BLANK10
替换为
10
空格等。

您可以在%%BLANK标记上拆分字符串。 之后,您可以读取任何令牌中的第一个数字,并将其转换为空格。
现在,您可以将每个令牌合并到一个新字符串中。

试试这个。我还没有完全测试过

$ awk '/BLANK/{ match($0,/%%BLANK([0-9]+)/,a);s=sprintf("%"a[1]"s","") ; gsub(a[0],s)}1' file
或红宝石(1.9+)

使用“%%BLANK”作为记录分隔符,现在如果有任何以数字开头的新记录,请将数字替换为空格

awk 'BEGIN {RS="%%BLANK";ORS=""}{MatchFound=match($0,"^[0-9]+",Matched_string);if(MatchFound){sub(Matched_string[0],"",$0);for (i=0;i<Matched_string[0];i++){$0=" "$0};print $0}else{print $0}}' InputFile.txt
awk'BEGIN{RS=“%%BLANK”;ORS=“}{MatchFound=match($0,“^[0-9]+”,匹配的字符串);if(MatchFound){sub(匹配的字符串[0],”,$0);for(i=0;i
perl -pe 's/%%BLANK(\d+)/" " x $1/e' input_file
awk 'BEGIN {RS="%%BLANK";ORS=""}{MatchFound=match($0,"^[0-9]+",Matched_string);if(MatchFound){sub(Matched_string[0],"",$0);for (i=0;i<Matched_string[0];i++){$0=" "$0};print $0}else{print $0}}' InputFile.txt