Using sed (or similar) to rename variable headings

Hello,

I'm rather new to the world of regular expressions and sed, though am excited by its possibilities. I have a particular task I'd like to achieve, and have googled the topic quite a bit. However, having found some codes that perform a task very similar to what I'd like to do, I can't for the life of me fathom how to adapt them. So I figured I'd try asking someone with a bit more experience.

Basically I have a dataset in a text file and I want to change the variable headings.

Currently they read:

p_1 p_2 p_3 ... p_19

and I'd like to change them to:

P[,1] P[,2] P[,3] ... P[,19]

Is this something that can be done using sed?
Thanks awfully!

Try this:

sed 's/_\([0-9][0-9]*\)/[,\1]/g' filename

cheers,
Devaraj Takhellambam

That's great - thanks very much!

Ideally I'd like to change the letters in the variable names from lower case to upper case. I've managed to do this by piping several sed commands together:

cat input.txt | sed 's/_\([0-9][0-9]*\)/[,\1]/g' | sed 's/p/P/g' | sed 's/d/D/g' >> output.txt

(there are only p's and d's in the variable names)

Out of curiosity (and because I'd like to try to learn more about the sed command), is there a way of achieving this with just one sed command?

Thanks!