{"id":3490,"date":"2010-09-23T19:02:00","date_gmt":"2010-09-23T19:02:00","guid":{"rendered":""},"modified":"2026-04-18T22:53:15","modified_gmt":"2026-04-18T22:53:15","slug":"shell-script-looping-files-in-a-directory-search-and-replace-text-in-each-file","status":"publish","type":"post","link":"https:\/\/hellem.org\/wblog\/?p=3490","title":{"rendered":"Shell script looping files in a directory, search and replace text in each file"},"content":{"rendered":"<p>Had to create a shell script that is looping through a set of XML files in a given directory. For each file it checks if the file has the attribute &#8220;<a href=\"http:\/\/en.wikipedia.org\/wiki\/CCSID\">CCSID<\/a>&#8220;. It it does not have the attribute, it inserts the attribute just after the opening tag MQQueueConnectionFactory using <a href=\"http:\/\/no.wikipedia.org\/wiki\/Perl\">Perl<\/a>, and if it is there already it will update the value using a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sed\">SED<\/a>-command with a simple <a href=\"http:\/\/en.wikipedia.org\/wiki\/Regular_expression\">REGEX<\/a>-pattern. Ok, I know that I could have used either SED or Perl for both, but I just borrowed from some other scripts I had, so that is the reason.<\/p>\n<p>The REGEX-pattern ain&#8217;t no rocket science, but since I use REGEX only once in a while I find it quite hard to crate patterns, could not have done it without http:\/\/www.gskinner.com\/RegExr\/ <\/p>\n<p><!-- codeblock lang=\"\" line=\"1\" --><\/p>\n<pre><code>  FILES=\/path_to_directory\/my-files*.xml\r\n  for file in $FILES\r\n  do\r\n    # CCSID attribute present or not\r\n    grep CCSID $file  &amp;gt;&amp;gt; \/dev\/null\r\n    if [ $? -eq 1 ]\r\n    then\r\n      # Not present, insert\r\n      perl -pi -e \"s#&amp;lt;MQQueueConnectionFactory\\s#&amp;lt;MQQueueConnectionFactory CCSID=\\\"1208\\\" #g\" ${file}\r\n    else\r\n      # Present, update\r\n      sed -i 's\/CCSID=\"[0-9]*\"\/CCSID=\"1208\"\/' ${file}\r\n    fi\r\n  done \r\n<\/code><\/pre>\n<p><!-- \/codeblock --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Had to create a shell script that is looping through a set of XML files in a given directory. For each file it checks if the file has the attribute &#8220;CCSID&#8220;. It it does not have the attribute, it inserts the attribute just after the opening tag MQQueueConnectionFactory using Perl, and if it is there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-3490","post","type-post","status-publish","format-standard","hentry","category-work-geek"],"_links":{"self":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3490"}],"version-history":[{"count":1,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3490\/revisions"}],"predecessor-version":[{"id":14113,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3490\/revisions\/14113"}],"wp:attachment":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}