Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

change character with "/" in a file

SOLVED
Go to Solution
Highlighted
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

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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

3 REPLIES
Acclaimed Contributor 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...

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

Acclaimed Contributor Acclaimed Contributor

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