<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gablog &#187; bash</title>
	<atom:link href="http://gabo.homelinux.com/en/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://gabo.homelinux.com</link>
	<description>Fino ad ora ho avuto un sito brutto. Ora ho un blog.</description>
	<lastBuildDate>Fri, 13 Nov 2009 14:29:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Recursive regular experession substitution</title>
		<link>http://gabo.homelinux.com/en/2009/02/sostituzione-ricorsiva-di-regular-expression/</link>
		<comments>http://gabo.homelinux.com/en/2009/02/sostituzione-ricorsiva-di-regular-expression/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 10:38:48 +0000</pubDate>
		<dc:creator>gabo</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[ricorsione]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://gabo.homelinux.com/?p=67</guid>
		<description><![CDATA[Script che sostituisce _ricorsivamente_ tutte le occorrenze della regexp passata come primo parametro con la regexp passata come secondo parametro, a partire dalla dir attuale o da quella specificata come terzo parametro]]></description>
			<content:encoded><![CDATA[<p>I found the following script very useful a lot of time. It can recursively substitute regexp passed as first argument whith the second one. The core of the script is the chstr_ric function:</p>

<pre class="brush: bash;">
function chstr_ric () {

  echo -e &quot;Num par: $#\n$0\n$1\n$2\n$3\n&quot;

# se non è specificata la dir di partenza parto da quella attuale
  if [ -z &quot;$3&quot; ]; then
    DIR=$PWD
  else
    DIR=$3
  fi

# Entro nella dir
  echo -e &quot;directory root: $DIR\n&quot;
  cd $DIR

  for FILE in * ; do
    if [ -d $FILE ] ; then
      echo -e &quot;\n*************\nEntro dentro a: $DIR/$FILE\n*************&quot;
# Se è una dir ci entro dentro e chiamo la ricorsione
      cd $FILE
      chstr_ric &quot;$1&quot; &quot;$2&quot; &quot;$FILE&quot;
      echo -e &quot;\n*************\nEsco da a: $DIR/$FILE\n*************&quot;
# finita la ricorsione risalgo nell albero delle directory
      cd ..
    else
      echo -e &quot;\tProcesso il file: $FILE&quot;
# se è un file attuo la sostituzione
      sed -e &quot;s/$1/$2/g&quot; $FILE &gt; $FILE.newsed
      mv -f $FILE.newsed $FILE
    fi
  done
}
</pre>

<p>It checks the third parameter, and if it's present it's considered like the starting directory. If not, actual directory is set as the root. After that is scans all the files and:
<ul>
	<li>If it finds a file it starts the substitution using sed and a temporary file</li>
	<li>If it finds a directory, then it calls himself recursively using the third parameter</li>
</ul>
The script is downloadable <a title="subst.sh" href="/download/subst.sh">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://gabo.homelinux.com/en/2009/02/sostituzione-ricorsiva-di-regular-expression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
