Discussion:
text manipulation query (copy column and manipulate second)
(too old to reply)
n***@gmail.com
2016-02-02 17:19:50 UTC
Permalink
So I have a file with the following:

test.txt:

first1.mid1.last1
first2.last2



I'd like to convert it as follows:

first1.last1 first1 mid1 last1
first2.last2 first2 last2


However this is failing...

awk '{$1=$1" " sub(/\./," ",$1)}{ print }' test.txt > test2.txt

I guess it's replacing the $1 field altogether... is there a way I can do this in another method?
Sven Probst
2016-02-16 20:28:24 UTC
Permalink
Post by n***@gmail.com
first1.mid1.last1
first2.last2
first1.last1 first1 mid1 last1
first2.last2 first2 last2
However this is failing...
awk '{$1=$1" " sub(/\./," ",$1)}{ print }' test.txt > test2.txt
I guess it's replacing the $1 field altogether... is there a way I can do this in another method?
You may try this:

awk '{n=split($0,a,/\./);gsub(/\./," ",$1)} { printf "%s.%s
%s\n",a[1],a[n],$1}' test.txt

first1.last1 first1 mid1 last1
first2.last2 first2 last2

Best regards,

Sven

Loading...