Operating System - HP-UX
1827871 Members
1222 Online
109969 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."
Rodney Hills
Honored Contributor

Re: Changing required field in file

Looks like I need to seperate the options-

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

-- Rod Hills

There be dragons...
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

perl -p -i -e s{(/vgCTSPR\D+)/}{$1.SNAP/} EC3TCD_final.map_file.orig
sh: Syntax error: `(' is not expected.

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

You may want to copy the script from here, rather then typing. There are curly braces which kind of look like parenthesis.

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

-- Rod Hills

There be dragons...
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

The above one just chaning for one particular string, which has "log" in it, not other lines got changed..
Thanks
Praqshant
"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

That's odd, I ran it against your file and got-

/dev/vgCTSPRD-log/rv500-logb2-10 /dev/vgEC3TCD3.SNAP/rv500-logb2-10 510976
/dev/vgCTSPRD1.SNAP/rv20-cntrlb-1 /dev/vgEC3TCD1/rv20-cntrlb-1
/dev/vgCTSPRD2.SNAP/rv20-cntrlb-2 /dev/vgEC3TCD2/rv20-cntrlb-2
/dev/vgCTSPRD3.SNAP/rv1000-17 /dev/vgEC3TCD3/rv1000-17 1024000
/dev/vgCTSPRD4.SNAP/rv1000-25 /dev/vgEC3TCD4/rv1000-25 1024000
/dev/vgCTSPRD5.SNAP/rv2000-0362 /dev/vgEC3TCD5/rv2000-0362 2048000

Which is what I thought what you were looking for.

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Changing required field in file

If you don't want lines with "log" changed, then do the following-

perl -p -i -e 's{(^/dev/vg......\d+)/}{$1.SNAP/}' yourfile
There be dragons...
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

Yeah I wanted the output like above. Thanks a lot for your help. Is it then difference in perl version or so? Not sure, but only changes lines with "log", i want o/p exactly like above.
Thanks
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
Prashant Zanwar_4
Respected Contributor

Re: Changing required field in file

Sorry for that, perl statement did the trick this time. I was surely doing something wrong here I guess. I will change lines with -log using vi, not a issue.
Also I got it working using awk..so one more thing..
grep -i ^/ EC3TCD_final.map_file.orig | awk -F/ '{ print "/dev/" $3 ".SNAP/" $4 " " "/dev/" $6 "/" $7 }'

Thanks all for excellant help.
Best regards
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."