Operating System - HP-UX
1752796 Members
5697 Online
108789 Solutions
New Discussion

change character with "/" in a file

 
SOLVED
Go to solution
support_billa
Valued Contributor

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

3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

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...

support_billa
Valued Contributor

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

James R. Ferguson
Acclaimed Contributor
Solution

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...