Issue
I have this following data set in one file (.txt file)
data1 = 275736 490;data11 = 87551 1004; data2 = 344670 4875; data3 = 472996 840;data4 = 0 0;data = 19708 279;data6 = 10262 18;data7 = 0 0;data8 = 428 6;data9 = 5986 11;data10 = 15114 173;data11 = 7483 106;data = 15900 25;
I want replace this digit space digit pattern (for example 472996 840) to digit,digit pattern (472996,840). This has to be done for 0 0 also as 0,0. values shouldn't be changed. I cannot replace all white space since other whitespace are needed as well. I have to replace white space between the digits to another string.
Any suggestions using tr/sed/awk ?
Tried this :
sed -i 's/\d+\s\d+/\d+\d+/g' comment3.txt
Also, in tr looks like we cannot match a pattern
Solution
An easy way is to capture the digit before the space and the digit that follows it, then reinsert the digits with a comma in between using the first and second backreference \1
and \2
. You would use the normal substitution form of sed 's/find/replace/'
adding g
to make the replacements global (replace all). For example:
sed -E 's/([0-9])[ ]([0-9])/\1,\2/g' file
That will take your data, e.g.
data1 = 275736 490;data11 = 87551 1004; data2 = 344670 4875; data3 = 472996 840;...
and convert the line to:
data1 = 275736,490;data11 = 87551,1004; data2 = 344670,4875; data3 = 472996,840;...
You can add the -i
to "edit-in-place" when you are satisfied it does what you intend.
Answered By - David C. Rankin Answer Checked By - Pedro (WPSolving Volunteer)