我有一些带有开始和结束标记的字符串,我想在一行上打印出来。 情况是:
例如,我要转换:
(start) AAAA
(the end)
(start) BBBB (the end)
(start) CCCC (the
end)
进入输出:
(start) AAAA (the end)
(start) BBBB (the end)
(start) CCCC (the end)
目前我有这个:
awk '/^\(start\)/{printf $0" ";next;}1' test.text
(start) AAAA (the end)
(start) BBBB (the end) (start) CCCC (the end)
此命令的问题是:
有什么好的工具和解决方案? 我会为正确方向的指针感到高兴。
答案 0 :(得分:0)
与其立即打印行,不如将行内容收集到一个变量中。到达结束标记时,请删除多余的空格并进行打印。
const noPhones: Phone[] = [];
get(userInfo, "phone", noPhones).map((item, index) => "Number: " + item.num); // okay
答案 1 :(得分:0)
请您尝试以下操作(此操作不会解决不平整的空间)。
incomplete and skewed building
要获得适当的空间,请尝试:
awk 'NF{printf("%s%s",$0,$0!~/\(the end\)$/?OFS:ORS)}' Input_file
输出如下。
awk 'NF{$1=$1;printf("%s%s",$0,$0!~/\(the end\)$/?OFS:ORS)}' Input_file
答案 2 :(得分:0)
这可能对您有用(GNU sed):
sed '/(start).*(the end)/b;/(start)/{:a;N;s/\n//;/(the end)/!ba}' file
如果一行同时包含开始条件和结束条件,则将其打印出来。否则,如果一行包含开始条件,请追加以下行,删除它们之间的换行符并测试结束条件。如果结束条件测试失败,请重复执行此操作,否则请打印当前修改的行。
在最初的问题中,预期的解决方案还删除了空行,可以在空白处添加
sed '/\S/!d;/(start).*(the end)/b;/(start)/{:a;N;s/\n//;/(the end)/!ba}' file
一种替代的,略短的解决方案:
sed '/\S/!d;/(start)/{:a;/(the end)/!{N;s/\n//;ba}}' file