[英]Skipping a part of a line using sed
I have a file with content like so - @1: 00001109
Each line is of the same format.我有一个包含这样内容的文件 - @1: 00001109
每一行的格式都相同。 I want the final output to be @1: 00 00 11 09
.我希望最终输出为@1: 00 00 11 09
。
I used command in sed to introduce a space every 2 characters - sed 's/.\\{2\\}/& /g'
.我在 sed 中使用命令每 2 个字符引入一个空格 - sed 's/.\\{2\\}/& /g'
。 But that will give me spaces in the part before the colon too which I want to avoid.但这也会在冒号之前的部分给我空格,我想避免。 Can anyone advise how to proceed?谁能建议如何继续?
Could you please try following, written and tested with shown samples.您能否尝试使用所示示例进行以下,编写和测试。
awk '{gsub(/../,"& ",$2);sub(/ +$/,"")} 1' Input_file
Explanation: First globally substituting each 2 digits pair with same value by appending space to it where gsub
is globally substitution to perform it globally).解释:首先通过在gsub
是全局替换以全局执行它的情况下附加空格来全局替换具有相同值的每个 2 位数字对)。 Once this is done, using single sub
to substitute last coming space with NULL to avoid spaces at last of lines.完成此操作后,使用 single sub
将最后一个空格替换为 NULL 以避免在最后一行出现空格。
With sed
:使用sed
:
sed -E 's/[0-9]{2}/& /g;s/ +$//' Input_file
Explanation: Globally substituting each pair of digits with its same value and appending spaces to it.说明:全局替换每一对数字,并用相同的值对其附加空格。 Then substituting space coming last space of line(added by previous substitution) with NULL.然后用NULL替换行的最后一个空格(由先前替换添加)的空格。
This might work for you (GNU sed):这可能对你有用(GNU sed):
sed 's/[0-9][0-9]\B/& /g' file
After a pair of digits within a word, insert a space.在单词中的一对数字后插入一个空格。
如果perl
恰好是您的选择,那么如何:
perl -pe '1 while s/(\d+)(\d\d)/$1 $2/g' file
you can use pure bash:您可以使用纯 bash:
for line in "$(<your_file.txt)"; do
first=`echo $line | cut -d' ' -f1`" "
last=`echo $line | cut -d' ' -f2`
for char in `seq 0 2 ${#last}`; do
first+=${last:$char:2}" "
done;
done;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.