Skip to content
Advertisement

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. 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?

Advertisement

Answer

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.

Advertisement