Reputation: 21
I have a file with content like so - @1: 00001109
Each line is of the same format. I want the final output to be @1: 00 00 11 09
.
I used command in sed to introduce a space every 2 characters - 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?
Upvotes: 0
Views: 182
Reputation: 22042
If perl
happens to be your option, how about:
perl -pe '1 while s/(\d+)(\d\d)/$1 $2/g' file
Upvotes: 1
Reputation:
you can use pure 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;
Upvotes: 0
Reputation: 58488
This might work for you (GNU sed):
sed 's/[0-9][0-9]\B/& /g' file
After a pair of digits within a word, insert a space.
Upvotes: 2
Reputation: 133680
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). Once this is done, using single sub
to substitute last coming space with NULL to avoid spaces at last of lines.
With 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.
Upvotes: 2