# Sed: replace particular one/two digit(s)

I have data in a table. And in one column I need to replace some indices (numbers from 1 to 23) to 3-4 digits sample numbers like: 2347,3856 etc.

So the original column looks like:

``````SF=1,2
SF=12,7
SF=17,4
``````

And the output should be:

``````SF=2347,3856
SF=8553,9539
...
``````

The problem is: if I do it for one sample (1->2720) it's fine; but then replacing 2 with something will create a mess etc., and I don't see how can I specify that the number should be replaced if only it's a single(two) digit for example? There could be more than 2 numbers in a row.

Sorry if it sounds confusing. I'm a beginner.

Update. Thanks fo mgjk I succeeded partially. But for some reason the new line is created after the first replacement in a row and then the next number is not replaced obviously. So what I get is:

``````SF=2347
``````

,2

``````SF=8553
``````

,7

And here is the code:

'sed "s/=\${index},/=\${sample},/g; s/,\${index} /,\${sample} /g; s/,\${index},/,\${sample},/g" samples.txt'

Why does it happen?

• Please provide also expected output so that we get a better idea of what you are trying to achieve. – peterph Oct 2 '14 at 13:26

## 1 Answer

I think, you're trying to do something like this:

``````\$ sed 's/=1,/=2347,/g; s/=12,/=3856,/g' sedtest.txt
SF=2347,2
SF=3856,15
SF=17,4
``````

Including the literal '=' and ',' in your pattern and replacement string would let you distinguish between a "=1," and an "=12,"

Update:

To replace both variables, in sed, you can use a \$ to match the end of the line.

``````\$ sed 's/=1,/=2347,/g; s/=12,/=3856,/g; s/,2\$/,2342/g; s/,15\$/,234325/g' sedtest.txt
SF=2347,2342
SF=3856,234325
SF=17,4
``````

It's going to look weird when you have bash variables like \${var} mixed in, but it should work, provided you use double-quotes:

e.g.,

``````\$ a=42
\$ sed "s/=1,/=2347,/g; s/=12,/=\${a},/g; s/,2\$/,2342/g; s/,15\$/,234325/g" sedtest.txt
SF=2347,2342
SF=42,234325
SF=17,4
``````

As an aside, if your replacements are huge, you can clean all this up using files in sed. E.g,

``````\$ cat ./datascript.sed
# Sed script to do stuff
s/=1,/=2347,/g
s/=12,/=2342,/g
s/,2\$/,2342/g
s/,15\$/,234325/g
``````

Then run the file against the data

``````\$ sed -f datascript.sed sedtest.txt
SF=2347,2342
SF=2342,234325
SF=17,4
``````

The downside to the files is that I don't know any way to use variables in them.

• Thank you so much! It works;) My only problem now is how to make several replacements in one line. If I do it like: 'sed "s/=\${index},/=\${sample},/g; s/,\${index} /,\${sample} /g; s/,\${index},/,\${sample},/g" samples.txt' then it makes a new line after the first number replaced and don't change the rest... – Eugenie Oct 7 '14 at 13:21
• sorry I don't see how to mark the code... – Eugenie Oct 7 '14 at 13:22
• Thank you very much for such explicit answer, mgjk! I didn't know about the possibility to use .sed script and it's really nice. But I am still trying to cope with last numbers... The thing is it is just one column from a table. And it seems there is no way to match the end of text in a column (which is not the end of line really). Probably I should extract this column, make replacements and then merge with a rest. Thank you so much for your support! – Eugenie Oct 8 '14 at 13:40