C. Monster C. Monster - 4 months ago 12
Bash Question

Using sed on a single column

Here is a command I'm running
It takes anything of the form (a number)(b or q) //a number followed by a b or a q
And replaces it with either b or q

cat Table1.txt | sed -r 's/[0-9]([bq])/\1/g'

ex. 8q -(changes to)> q
7b -(changes to)> b


But I wan't this to be done only on the second column so

tyek8qk 9b88 column
uel76qk 8q76 word


Would change to

tyek8qk b88 column
uel76qk q76 word


This is my attempt so far but it just produces exactly what is input

cat Table1.txt | sed -r '2,2s/[0-9]([bq])/\1/g'

Answer

You can use this sed:

sed -E 's/([^[:blank:]]+[[:blank:]]+)[0-9]([bq])/\1\2/' file

tyek8qk b88 column
uel76qk q76 word

If you have gnu awk then can do this:

awk '{$2=gensub(/[0-9]+([bq])/, "\\1", 1, $2)} 1' file
Comments