user1116360
user1116360

Reputation: 422

Split specific column(s)

I have this kind of recrods:

1 2 12345
2 4 98231
...

I need to split the third column into sub-columns to get this (separated by single-space for example):

1 2 1 2 3 4 5
2 4 9 8 2 3 1

Can anybody offer me a nice solution in sed, awk, ... etc ? Thanks!

EDIT: the size of the original third column may vary record by record.

Upvotes: 1

Views: 341

Answers (7)

potong
potong

Reputation: 58351

This might work for you:

echo -e "1 2 12345\n2 4 98231" | sed 's/\B\s*/ /g'
1 2 1 2 3 4 5
2 4 9 8 2 3 1

Most probably GNU sed only.

Upvotes: 0

kev
kev

Reputation: 161604

$ awk -F '' '$1=$1' data.txt | tr -s ' '
1 2 1 2 3 4 5
2 4 9 8 2 3 1

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77075

Using perl:

perl -pe 's/([0-9])(?! )/\1 /g' INPUT_FILE

Test:

[jaypal:~/Temp] cat tmp
1 2 12345
2 4 98231
[jaypal:~/Temp] perl -pe 's/([0-9])(?! )/\1 /g' tmp
1 2 1 2 3 4 5 
2 4 9 8 2 3 1 

Using gnu sed:

sed 's/\d/& /3g' INPUT_FILE

Test:

[jaypal:~/Temp] sed 's/[0-9]/& /3g' tmp
1 2 1 2 3 4 5 
2 4 9 8 2 3 1 

Using gnu awk:

gawk '{print $1,$2,gensub(/./,"& ","G", $NF)}' INPUT_FILE

Test:

[jaypal:~/Temp] gawk '{print $1,$2,gensub(/./,"& ","G", $NF)}' tmp
1 2 1 2 3 4 5 
2 4 9 8 2 3 1 

Upvotes: 2

Ismael Luceno
Ismael Luceno

Reputation: 2119

If you don't care about spaces, this is a succinct version:

sed 's/[0-9]/& /g'

but if you need to remove spaces, we just chain another regexp:

sed 's/[0-9]/& /g;s/   */ /g'

Note this is compatible with the original sed, thus will run on any UNIX-like.

Upvotes: 1

user982733
user982733

Reputation:

Using awk substr and printf:

[srikanth@myhost ~]$ cat records.log 
1 2 12345 6 7
2 4 98231 8 0

[srikanth@myhost ~]$ awk '{ len=length($3); for(i=1; i<=NF; i++) { if(i==3) { for(j = 1; j <= len; j++){ printf substr($3,j,1) " "; } } else { printf $i " "; } } printf("\n"); }' records.log 
1 2 1 2 3 4 5 6 7 
2 4 9 8 2 3 1 8 0 

You can use this for more than three column records as well.

Upvotes: 2

johnsyweb
johnsyweb

Reputation: 141780

Awk

% echo '1 2 12345
2 4 98231
...' | awk '{
    gsub(/./, "& ", $3)
    print
}
'
1 2 1 2 3 4 5
2 4 9 8 2 3 1
...

[Tested with GNU Awk 3.1.7]

This takes every character (/./) in the third column ($3) and replaces (gsub()) it with itself followed by a space ("& ") before printing the entire line.

Upvotes: 6

Eduardo Ivanec
Eduardo Ivanec

Reputation: 11852

Sed solution:

sed -e 's/\([0-9]\)/\1 /g' -e 's/ \+/ /g'

The first sed expression replaces every digit with the same digit followed by a space. The second expression replaces every block of spaces with a single space, thus handling the double spaces introduced by the previous expression. With non-GNU seds you may need to use two sed invocations (one for each -e).

Upvotes: 3

Related Questions