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

2

3

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

Miniskirt answered 31/3, 2021 at 4:57 Comment(4)
please do add your efforts(in form of code) in your question which is highly encouraged on SO, thank you.Peery
sed -E 's/([0-9])[ ]([0-9])/\1,\2/g' file then add -i when satisfied it does what you intend.Krueger
See stackoverflow.com/tags/sed/info for learning resourcesRepletion
Indeed, tr is wholly unsuitable for this. I (finally) updated the tr tag info page with a note about this.Shephard
K
4

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.

Krueger answered 31/3, 2021 at 5:13 Comment(2)
sed -E 's/([0-9])[ ]([0-9])/\1,\2/g' file So here \1 and \2 in 'replace' section refers to matching sub expression of 'find' section right ?Miniskirt
Correct, each capture group (...) will capture what is between the parentheses and \1 reinserts the information from the first group, \2 from the second, and so on. If you did not have extended regular expressions available for your sed you would escape the capture groups using Basic Regular Expressions, e.g. sed 's/\([0-9]\)[ ]\([0-9]\)/\1,\2/g' file (it just looks busier, but does the exact same thing)Krueger
P
1

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
Peery answered 31/3, 2021 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.