我有以下问题。我正在解释一个输入文件,现在我遇到了这个问题:我需要翻译%%BLANKx
成x
空格。
因此,无论在输入文件中的哪个位置,我发现例如%%BLANK8
,我需要%%BLANK8
用8
空格、%%BLANK10
空格10
等替换。
using "%%BLANK" as record seperator , now if any new record that starts with a number replace the number with spaces.
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
perl -pe 's/%%BLANK(\d+)/" " x $1/e' input_file
尝试这个。我没有彻底测试
$ awk '/BLANK/{ match($0,/%%BLANK([0-9]+)/,a);s=sprintf("%"a[1]"s","") ; gsub(a[0],s)}1' file
或红宝石(1.9+)
$ ruby -ne 'print $_.gsub(/%%BLANK(\d+)/){|m|" "*$1.to_i}' file
您可以在 %%BLANK 标记上拆分您的字符串。之后,您可以读取任何令牌中出现的第一个数字并将其转换为空格。现在,您可以将每个标记连接到一个新字符串中。