Register
It is currently Thu Dec 18, 2014 3:52 am

Replacing Windows-Strings with Linux-String


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Jul 05, 2010 5:12 am   

Joined: Mon Jul 05, 2010 5:01 am
Posts: 1
Hi all.
I'm in the unfortunate position that I have to work with Windows and Linux systems using MATLAB. For my MATLAB program I have config-files that contain the pathes to the corresponding data, etc. Now to switch from Windows to Linux and back, I'm trying to write a script that replaces the paths for the datasets. It is supposed to this recursivley for all configs contained in the 'data_analysis' directory.
Now it does work replacing simple strings (i.e.: for a simple variable), but with Windows- and Linux-paths it doesn't which has to do with the '\', ':', etc.
I call the script in the following form:
>> replace_string.sh C:\data_analysis /media/eclipse_ti/data_analysis/ /media/eclipse_ti/data_analysis/
where the 1. argument is the string to replace, 2. the string that will be put, 3. where to replace it.
I hope somebody can help me out and perhaps help me create a script that's as robust as possible.
Thanks in advance,
Mike

Code:
#!/bin/bash
directory=$3
includedFiles=$4
stringToReplace=$1
replacementString=$2
currentDate=`(date +%Y%m%d)`

echo $directory
echo $#
echo $stringToReplace
#echo $replacementString

if [ $# -eq 4 ]
then
   fileList=`grep --include=$includedFiles -RHl $stringToReplace $directory`
#   echo "grep --include=$includedFiles -RHl $stringToReplace $directory"
fi

if [ $# -eq 3 ]
then
   fileList=`grep -RHl $stringToReplace $directory`
#   echo "grep -RHl $stringToReplace $directory"
fi

for file in $fileList
do
   echo "Processing $file"
   sed --in-place="_StringReplacementBKP$currentDate" "s|$stringToReplace|$replacementString|g" $file
done


Top
 Profile  
 PostPosted: Mon Jul 05, 2010 4:03 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
You don't show how it doesn't work.

try like this:
Code:
#!/bin/bash
set -x

stringToReplace=$1
replacementString=$2
directory=$3
includedFiles=$4
#currentDate=`(date +%Y%m%d)`
currentDate=$(date +%Y%m%d)

echo "$directory"
echo "$#"
echo "$stringToReplace"
#echo $replacementString

#if [ $# -eq 4 ]
if (( $# == 4 ))
then
# use an array instead
#   fileList=`grep --include=$includedFiles -RHl $stringToReplace $directory`
   fileList=( $(grep --include="$includedFiles" -RHl "$stringToReplace" "$directory") )
#   echo "grep --include=$includedFiles -RHl $stringToReplace $directory"
#fi

#if [ $# -eq 3 ]
elif (( $# == 3 ))
then
#   fileList=`grep -RHl $stringToReplace $directory`
   fileList=( $(grep -RHl "$stringToReplace" "$directory") )
#   echo "grep -RHl $stringToReplace $directory"
fi

for file in "${fileList[@]}"
do
   echo "Processing $file"
#   sed --in-place="_StringReplacementBKP$currentDate" "s|$stringToReplace|$replacementString|g" $file
   sed --in-place="_StringReplacementBKP$currentDate" \
's|'"${stringToReplace//\\/\\\\}"'|'"$replacementString"'|g' "$file"
done
and paste the output if it doesn't work.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP