LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   grep gurus, I need your help (https://www.linuxquestions.org/questions/linux-software-2/grep-gurus-i-need-your-help-843608/)

qrange 11-11-2010 02:14 AM

grep gurus, I need your help
 
I'd like to edit txt file so that these lines:
p2snmpmod HDRx_05 address="10.20.30.40" subslo=5 slonum=0
p2snmpmod HDRx_06 address="10.20.30.40" subslo=6 slonum=0
p2snmpmod HDRx_07 address="10.20.30.40" subslo=7 slonum=0
..

become like this:
p2snmpmod HDRx_01 address="10.20.30.40" subslo=5 slonum=0
p2snmpmod HDRx_02 address="10.20.30.40" subslo=6 slonum=0
p2snmpmod HDRx_03 address="10.20.30.40" subslo=7 slonum=0
..

I'd do it manually but there is a lot of them, and I might need something similar again.
please help me out.

thanks.

colucix 11-11-2010 03:07 AM

Do you mean to renumber the second field starting from 1? Awk can easily do that, e.g.
Code:

awk '/^p2snmpmod/{sub(/_[0-9]+/,sprintf("_%02d",++count),$2); print}' file
but it strongly depends on the exact format of the whole file. Does the numbers exceed 99? Is the first field always "p2snmpmod"? Just to give you an idea.

qrange 11-11-2010 03:11 AM

thank you very much;
no, all numbers are <50;
yes it always starts with p2snmpmod.


All times are GMT -5. The time now is 08:27 PM.