Sed only digits not in numbers

Hi,

I have a text file with an array of numbers such as :

123    1    456   45   9817   1   45

I would like to replace the digit "1" in a text file with "A". So it looks like this:

123   A   456   45   9817  A  45

If I use sed 's/1/A/g' , I get

A23   A   456  45  98A7  A 45

I tried several combination such as [ 1 ] and others, to no avail.
What do I need to add to get the wanted result?
Thanks a lot!

Depending on your sed version:

sed 's/\b1\b/A/g' infile

or:

sed 's/\<1\>/A/g' infile

If none of them works:

perl -pe's/\b1\b/A/g' infile
1 Like

also possible with awk. Check each record for the string "1" and replace with "A". Won't replace inside a record.

awk '{ 'for(N=1; N<=NF; N++) if($N == "1") $N="A" } 1' < datafile
1 Like

Thanks Radoulov!

The first one worked!