Perl and Shell Scritping Windows

  • Code to list all Windows environment variables in current user shell

C:>perl -e “foreach (sort keys %ENV) {print “$_ = $ENV{$_}n”; } “; > Tempperl-env.txt

C:>

  • Single Command to combine all files in directory into single file.
    • C:Temptest>dir /w *.json | grep -i json
      tasklist-level-1.json   tasklist-level-2.json   tasklist-level-3.json
      tasklist-level-4.jsonC:Temptest>copy *.json combined-file.json
      tasklist-level-1.json
      tasklist-level-2.json
      tasklist-level-3.json
      tasklist-level-4.json
      1 file(s) copied.

      C:Temptest>dir /w *.json | grep -i json
      combined-file.json      tasklist-level-1.json   tasklist-level-2.json
      tasklist-level-3.json   tasklist-level-4.json

      C:Temptest>wc -l

      C:Temptest>wc -l combined-file.json
      2230 combined-file.json

      C:Temptest>

  • Single command line to find and replace string in all files of a folder.  Use Cygwin on Windows.
    • gunnalag@GMD /cygdrive/c/temp
      $ grep -r -i idle ./test
      ./test/tasklist-level-1.json:   “System idle Process”:{
      ./test/tasklist-level-2.json:   “System idle Process”:[
      ./test/tasklist-level-3.json:   “System idle Process”:[
      ./test/tasklist-level-4.json:   “System idle Process”:[
      gunnalag@GMD /cygdrive/c/temp
      $ perl -e “s/idle/ideal/ig;” -pi $(find test -type f)
      gunnalag@GMD /cygdrive/c/temp
      $ grep -r -i idle ./test
      ./test/tasklist-level-1.json.bak:   “System idle Process”:{
      ./test/tasklist-level-2.json.bak:   “System idle Process”:[
      ./test/tasklist-level-3.json.bak:   “System idle Process”:[
      ./test/tasklist-level-4.json.bak:   “System idle Process”:[
      gunnalag@GMD /cygdrive/c/temp
      $ grep -r -i ideal ./test
      ./test/tasklist-level-1.json:   “System ideal Process”:{
      ./test/tasklist-level-2.json:   “System ideal Process”:[
      ./test/tasklist-level-3.json:   “System ideal Process”:[
      ./test/tasklist-level-4.json:   “System ideal Process”:[
      gunnalag@GMD /cygdrive/c/temp
      $
    • Single command line to append a new line “with text” to all files in a directory
      • gunnalag@GMD /cygdrive/c/temp/test
        $ for FILE in *; do echo “Newly Added Line” >> $FILE; done

        gunnalag@GMD /cygdrive/c/temp/test
        $ tail -3 tasklist-level-1.json
        }
        “n”
        Newly Added Line

        gunnalag@GMD /cygdrive/c/temp/test
        $

    • h

Leave a Reply

Your email address will not be published. Required fields are marked *