搜索模式并用它创建新行

时间:2017-09-23 12:24:17

标签: awk

输入:

button.addEventListener('click',()=>{
  clearTimeout(AUTO_TIMER);
});

输出

2:this is a sentence 221j: 54: this another sentence: 4245: again a sentence 3:the last sentence

snipet应搜索“:”后面的1或2或3个数字。在':'之后取全部直到下一个模式,并用它创建一个新行。

3 个答案:

答案 0 :(得分:2)

awk 解决方案:

awk -F'[0-9]+:[[:space:]]*' '{for(i=1;i<=NF;i++) if($i) print $i}' inputfile
  • -F'[0-9]+:[[:space:]]*' - 复杂字段分隔符

输出:

this is a sentence 221j: 
this another sentence: 
again a sentence 
the last sentence

答案 1 :(得分:1)

这就是你所说的你想要的(替换1,2或3位数后跟:):

$ awk '{sub(/[0-9]{1,3}: */,""); gsub(/ *[0-9]{1,3}: */,ORS)}1' file
this is a sentence 221j:
this another sentence: 4
again a sentence
the last sentence

如果您真正想要的是替换样本输入/输出所指示的:之后的任何整数,那么它将是:

$ awk '{sub(/[0-9]+: */,""); gsub(/ *[0-9]+: */,ORS)}1' file
this is a sentence 221j:
this another sentence:
again a sentence
the last sentence

答案 2 :(得分:1)

你能不能试试下面的awk(在GNU awk中测试过),让我知道这是否对你有所帮助,这也仅在提供的示例Input_file上进行了测试。

awk -v RS='[0-9]+:' 'NF{gsub(/^ |\n/,"");print}'   Input_file

输出如下。

this is a sentence 221j:
this another sentence:
again a sentence
the last sentence

<强>解释

awk -v RS='[0-9]+:' ' ##Setting Record Separator as all the digits together with colon.
NF{                   ##Then checking if line is NOT blank by checking NF(number of fields) are NOT null, if yes then do following.
 gsub(/^ |\n/,"");    ##Globally substituting starting space and new line with NULL in current line.
 print                ##printing the current line.
}
'   Input_file        ##Mentioning the Input_file here.