Nizamudheen At
Nizamudheen At

Reputation: 334

How to substitute a pattern with another pattern in unix enviroment maintianing its values using sed/awk/tr?

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

Upvotes: 3

Views: 869

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133640

1st solution: With GNU awk's gensub function please try following.

awk '
{
  print gensub(/([0-9]+) +([0-9]+)/, "\\1,\\2", "g", $0)
}
' Input_file


2nd solution: With GNU awk, you could try following too.

awk -v RS='[0-9]+[[:space:]]+[0-9]+' '{sub(/[[:space:]]+/,",",RT);ORS=RT} 1' Input_file

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84579

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.

Upvotes: 4

Related Questions