Re: change character with "/" in a file (610 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 4 (618 Views)
Accepted Solution

change character with "/" in a file

hello,

 

what is the best way to change characters with "/" ( like a HPUX Volume Group ) in a file and also preserve the file permission, file owner ....

 

sed, awk or perl ?

 

i have some examples:

 

fs_act_vg=/dev/vgact

fs_new_vg=/dev/vgnew

 

SED:

 

sed "s|^/dev/${fs_act_vg}|/dev/${fs_new_vg}|g" file > file_new

cp file_new file

 

AWK:

but no finished ...
awk -v fs_act_vg="/dev/${fs_act_vg}" -v fs_new_vg="/dev/${fs_new_vg}" '$0 ~ fs_act_vg {print sub( $0,fs_act_vg,fs_new_vg) }' file > file_new
cp file_new file

 

PERL
perl -i.old -p -e "s/\/dev\/${fs_act_vg}/\/dev\/${fs_new_vg}/ if /\/dev\/${fs_act_vg}/" file

 

regards

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 4 (610 Views)

Re: change character with "/" in a file

Hi:

 

The best way?  Define "best" --- shortest number of characters?  Readability (assuming you know each language)?  Smallest number of processes?   Smallest process footprint?  Speed?

 

By the way, you can golf your perl script to eliminate the toothpicks (escaped forward slash characters) by changing the delimiter from '/' to balanced pairs of angle, round, square or curly brackets.

 

Regards!

 

...JRF...

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 3 of 4 (602 Views)

Re: change character with "/" in a file

The best way?  Define "best" --- shortest number of characters?  Readability (assuming you know each language)?

 

i know very well "sed", "awk" but when i want to change a content in a file , i always must work with a temporary file ?  a colleague ( he didn't work in our company) change "sed" to "perl" because we had some problems with "sed" and temporary files. so i think "perl" one liner are a good alternative ?

 

improvment : ( a better way for "if" i didn't find )

perl -i.old -p -e "s |/dev/${fs_act_vg}|/dev/${fs_new_vg}| if /\/dev\/${fs_act_vg}/ "

 

regards

 

regards

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 4 (597 Views)

Re: change character with "/" in a file

Hi (again):

 

The GNU 'sed' offers in-place editing akin to Perl.  The use of 'awk' wold require a temporary file.

 

perl -i.old -p -e "s |/dev/${fs_act_vg}|/dev/${fs_new_vg}| if /\/dev\/${fs_act_vg}/ "

...does not need the 'if' condition.  The substitution will occur only if there is a match of the first operand, so:

 

perl -pi.old -e "s|/dev/${fs_act_vg}|/dev/${fs_new_vg}|"

Regards!

 

...JRF...

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.