Skip to main content
was able to solve it with sed
Source Link

I'm using Ubuntu 11.04 and wrote a small script that searches within text files for certain "tokens" and replaces with some a prewritten snippet from a template file of the same name.

The text files that are being searched will have two and only two instances of each token. The first is a plain text and the second is an html version, and there are separate snippets for each.

Here's the script:

for f in `ls -1 .templates/template_text`;
do
    g=`cat .templates/template_text/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/";
done

for f in `ls -1 .templates/template_html`;
do
    g=`cat .templates/template_html/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/g";
done

I'm running into a problem where even when I don't have "global" specified in the first regex, it still replaces both tokens. I'm not sure if this is because of how I'm calling perl, a bug, or some other issue.

Any help would be appreciated.

UPDATE: I was able to get the script working by using sed instead of perl.

for f in `ls -1 .templates/template_text`;
do
    g=`cat .templates/template_text/$f`
    h=`cat .templates/template_html/$f`
    find to_process/ -type f -print0 | xargs -0 -I {} sed -i -e "0,/$f/s/$f/$g/" -e "0,/$f/s/$f/$h/" {}
done

Still interested in how to get it working with the perl command though.

I'm using Ubuntu 11.04 and wrote a small script that searches within text files for certain "tokens" and replaces with some a prewritten snippet from a template file of the same name.

The text files that are being searched will have two and only two instances of each token. The first is a plain text and the second is an html version, and there are separate snippets for each.

Here's the script:

for f in `ls -1 .templates/template_text`;
do
    g=`cat .templates/template_text/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/";
done

for f in `ls -1 .templates/template_html`;
do
    g=`cat .templates/template_html/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/g";
done

I'm running into a problem where even when I don't have "global" specified in the first regex, it still replaces both tokens. I'm not sure if this is because of how I'm calling perl, a bug, or some other issue.

Any help would be appreciated.

I'm using Ubuntu 11.04 and wrote a small script that searches within text files for certain "tokens" and replaces with some a prewritten snippet from a template file of the same name.

The text files that are being searched will have two and only two instances of each token. The first is a plain text and the second is an html version, and there are separate snippets for each.

Here's the script:

for f in `ls -1 .templates/template_text`;
do
    g=`cat .templates/template_text/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/";
done

for f in `ls -1 .templates/template_html`;
do
    g=`cat .templates/template_html/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/g";
done

I'm running into a problem where even when I don't have "global" specified in the first regex, it still replaces both tokens. I'm not sure if this is because of how I'm calling perl, a bug, or some other issue.

Any help would be appreciated.

UPDATE: I was able to get the script working by using sed instead of perl.

for f in `ls -1 .templates/template_text`;
do
    g=`cat .templates/template_text/$f`
    h=`cat .templates/template_html/$f`
    find to_process/ -type f -print0 | xargs -0 -I {} sed -i -e "0,/$f/s/$f/$g/" -e "0,/$f/s/$f/$h/" {}
done

Still interested in how to get it working with the perl command though.

Source Link

perl regex replacing globally when global not selected

I'm using Ubuntu 11.04 and wrote a small script that searches within text files for certain "tokens" and replaces with some a prewritten snippet from a template file of the same name.

The text files that are being searched will have two and only two instances of each token. The first is a plain text and the second is an html version, and there are separate snippets for each.

Here's the script:

for f in `ls -1 .templates/template_text`;
do
    g=`cat .templates/template_text/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/";
done

for f in `ls -1 .templates/template_html`;
do
    g=`cat .templates/template_html/$f`
    find to_process/ -type f | xargs perl -i.old -p -e "s/$f/$g/g";
done

I'm running into a problem where even when I don't have "global" specified in the first regex, it still replaces both tokens. I'm not sure if this is because of how I'm calling perl, a bug, or some other issue.

Any help would be appreciated.