匹配模式,并在每个字符后面插入此模式,直到行末。

huangapple go评论49阅读模式
英文:

Match pattern and insert this pattern after every character until end of line

问题

I've got a line with an identifier, then a pattern (in my case, a semicolon) and then a list of numbers:

echo "sp16;111111111111111111111111111111211"

I'd like to insert a semicolon between all numbers as in (desired output):

echo "sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1"

So far, I found how to insert a semicolon between all characters using sed:

sed 's/.\{1\}/&;/g'

But, then it also inserts semicolons before matching the first semicolon and also it adds a semicolon at the end of the line.

英文:

I've got a line with an identifier, then a pattern (in my case, a semicolon) and then a list of numbers:

echo "sp16;111111111111111111111111111111211" 

I'd like to insert a semicolon between all numbers as in (desired output):

echo "sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1"

So far, I found how to insert a semicolon between all characters using sed:

sed 's/.\{1\}/&;/g'

But, then it also inserts semicolons before matching the first semicolon and also it adds a semicolon ad the end of the line.

答案1

得分: 5

With a Perl的一行代码:

<p>

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

正则表达式匹配如下:

节点 解释
^ 字符串的开头锚点
( 组并捕获到 \1:
[^;]+ 除了 ; 之外的任何字符(1次或多次(匹配最大数量))
; ;
) \1 的结束
( 组并捕获到 \2:
[0-9]+ 任何字符:'0' 到 '9'(1次或多次(匹配最大数量))
) \2 的结束

Perl 的代码解释

运算符 含义
$1 捕获组 1,类似于 sed 中的 \1
. 连接
join ";" 使用字符 ';' 连接以下列表...
split "", $2 分割捕获组 $2 或 \2 中的数字为列表
e 修饰符,允许在替换 s/// 的右侧使用 Perl 代码/表达式
英文:

With a Perl's one-liner:

perl -pe &#39;s/^([^;]+;)([0-9]+)/$1 . join &quot;;&quot;, split &quot;&quot;, $2/e&#39; file

<p>

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

The regular expression matches as follows:

Node Explanation
^ the beginning of the string anchor
( group and capture to \1:
[^;]+ any character except: ; (1 or more times (matching the most amount possible))
; ;
) end of \1
( group and capture to \2:
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
) end of \2

Perl's code explanations

Operator Meaning
$1 captured group 1 like \1 in sed
. concatenation
join &quot;;&quot;, join with the character ';' the following list...
split &quot;&quot;, $2 split captured group $2 or \2 in a list of digits
e modifier that allow Perl's code/expression in the right part of substitution s///

答案2

得分: 1

使用GNU sed

echo "sp16;111111111111111111111111111111211" \
  | sed -E ':a; s/(;[^;])([^;]+)$/;/; ta'

输出:

<pre>
sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1
</pre>

参考:man sedThe Stack Overflow Regular Expressions FAQ

英文:

With GNU sed:

echo &quot;sp16;111111111111111111111111111111211&quot; \
  | sed -E &#39;:a; s/(;[^;])([^;]+)$/;/; ta&#39;

Output:
<pre>
sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1
</pre>

See: man sed and The Stack Overflow Regular Expressions FAQ

答案3

得分: 0

以下是翻译好的内容:

使用任何awk:

$ echo "sp16;111111111111111111111111111111211" |
    awk 'BEGIN{FS=OFS=";"} {gsub(/./,FS"&",$2); sub(FS,"")} 1'
sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1
英文:

Using any awk:

$ echo &quot;sp16;111111111111111111111111111111211&quot; |
    awk &#39;BEGIN{FS=OFS=&quot;;&quot;} {gsub(/./,FS&quot;&amp;&quot;,$2); sub(FS,&quot;&quot;)} 1&#39;
sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

答案4

得分: 0

以下是经过翻译的部分:

这是一个适用于任何sed版本的sed命令:

sed -E -e :a -e &#39;s/([0-9])([0-9]+)$/;/; ta&#39; file

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

这是一个建议的awk解决方案:

awk -F&#39;;&#39; -v OFS= &#39;{gsub(/./, FS &quot;&amp;&quot;, $2)} 1&#39; file

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1
英文:

Here is another sed command that will work for any sed version:

sed -E -e :a -e &#39;s/([0-9])([0-9]+)$/;/; ta&#39; file

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

Here is a suggested awk solution:

awk -F&#39;;&#39; -v OFS= &#39;{gsub(/./, FS &quot;&amp;&quot;, $2)} 1&#39; file

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

答案5

得分: 0

这可能适用于您(GNU sed):

sed 's/;/\n/;h;s/\B/;/g;H;x;s/\n.*\n/;/&#39; file

分而治之。

替换第一个分隔符;为换行符并创建一个副本。

在每个单词字符之间插入;

将结果附加到原始副本。

用引入的两个换行符之间的所有内容替换为;


如果第一个标识符始终为4个字符:

sed 's/./;&amp;/7g' file
英文:

This might work for you (GNU sed):

sed &#39;s/;/\n/;h;s/\B/;/g;H;x;s/\n.*\n/;/&#39; file

Divide and conquer.

Replace the first delimiter ;, by a newline and make a copy.

Insert a ; between each in-word character.

Append the result to original copy.

Replace everything between the two introduced newlines by ;.


If the first identifier is always 4 characters:

sed &#39;s/./;&amp;/7g&#39; file

答案6

得分: 0

"sp16;111111111111111111111111111111211"
"sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1"

英文:
echo &quot;sp16;111111111111111111111111111111211&quot; | 

mawk &#39;gsub(&quot;.&quot;, &quot;;&amp;&quot;, $NF)&#39; OFS= FS=&#39;;&#39; 

sp16;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1

huangapple
  • 本文由 发表于 2023年5月7日 00:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定