Operating System - HP-UX
1753297 Members
6917 Online
108792 Solutions
New Discussion юеВ

Changing required field in file

 
Prashant Zanwar_4
Respected Contributor

Changing required field in file

Hi,
I have some file which has all volumes of a particular field listed.
Fields are:
vg1
vg2
..
..
..
vg10

I want to change all these by
vg1.SNAP
vg2.SNAP
..
..
vg10.SNAP

Is it possible to do it one shot. I can do it using vi or sed 1 field at a time.
Thanks and regards
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
17 REPLIES 17
Kent Ostby
Honored Contributor

Re: Changing required field in file

awk '/^vg/{print $0".SNAP"}' < inputfile > outputfile

This assume that NO other lines start with lowercase vg.

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

OK sorry..I put it wrongly.
I have

/dev/vg1/rv... /dev/vg1/rv...
/dev/vg2/rv... /dev/vg2/rv...
..
..
..
/dev/vg10/rv.../dev/vg2/rv...

Like this above. I didnt put rv completly, they are raw vols. I want to change ENVNAME[1-10] to
ENVNAME[1-10].SNAP (please note (ENV and env are different.)

Thanks
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Rajeev Tyagi
Valued Contributor

Re: Changing required field in file

Prashant,

Lets say your filename is /tmp/test. You can use one command to replace this.

sed 's/ENVNAME\>[1-9]/&.SNAP/' /tmp/test

Rajeev Tyagi
Valued Contributor

Re: Changing required field in file

Prashant,

One correction you may have issue with vg10. You can use

sed 's/ENVNAME\>[1-9][0]/&.SNAP/' /tmp/test | sed 's/ENVNAME\>[1-9]/&.SNAP/' | sed 's/\.SNAP0/0/'

Hope this helps.
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

thats not working rajeev..
Thanks
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Rajeev Tyagi
Valued Contributor

Re: Changing required field in file

Prashant,

I used your format in /tmp/test file

/dev/vg1/rv... /dev/vg1/rv...
/dev/vg2/rv... /dev/vg2/rv...
/dev/vg3/rv... /dev/vg2/rv...
/dev/vg4/rv... /dev/vg2/rv...
/dev/vg5/rv... /dev/vg2/rv...
/dev/vg6/rv... /dev/vg2/rv...
/dev/vg7/rv... /dev/vg2/rv...
/dev/vg8/rv... /dev/vg2/rv...
/dev/vg9/rv... /dev/vg2/rv...
/dev/vg10/rv.../dev/vg2/rv...

And I get following output with

#sed 's/ENVNAME\>[1-9][0]/&.SNAP/' /tmp/test | sed 's/ENVNAME\>[1-9]/&.SNAP/' | sed 's/\.SNAP0/0/'

/dev/vg1.SNAP/rv... /dev/vg1/rv...
/dev/vg2.SNAP/rv... /dev/vg2/rv...
/dev/vg3.SNAP/rv... /dev/vg2/rv...
/dev/vg4.SNAP/rv... /dev/vg2/rv...
/dev/vg5.SNAP/rv... /dev/vg2/rv...
/dev/vg6.SNAP/rv... /dev/vg2/rv...
/dev/vg7.SNAP/rv... /dev/vg2/rv...
/dev/vg8.SNAP/rv... /dev/vg2/rv...
/dev/vg9.SNAP/rv... /dev/vg2/rv...
/dev/vg10.SNAP/rv.../dev/vg2/rv...

Please let me know if this is not your expected output, or if you are having syntax error with above command?

Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

I am attaching file here. I have done 10,11,12,13 manually. Still it will be helpful for me if there is a direct way. I dont have errors, it is generating same output as input.
Thanks and regards
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Rodney Hills
Honored Contributor

Re: Changing required field in file

A one line perl call-

perl -pie 's{(/vg......\d+)/}{$1.SNAP/}' yourfile

This will apply the change directly to "yourfile" so you may want a backup of it before trying this routine. Since your ENVNAME may have digits, I'm assuming that they are 6 characters long.

HTH

-- Rod Hills

There be dragons...
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

The perl command throws error
( not expected and also if I remove that 1 not set..

Thx
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."