Skip to content

Instantly share code, notes, and snippets.

@TyounanMOTI
Created February 17, 2011 17:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TyounanMOTI/832132 to your computer and use it in GitHub Desktop.
Save TyounanMOTI/832132 to your computer and use it in GitHub Desktop.
'sed -e' for each file. Output result for each file.
#!/bin/bash
# esed 's/foo/bar/g' files....
script=$1
shift 1
CHANGED=1 # diff returns 1 for differences. (0 for no difference, 2 for errors.)
for file in $*
do
sed -e ${script} ${file} > ${file}~
diff ${file} ${file}~ > /dev/null
if [ $? -eq ${CHANGED} ]; then
cp -p ${file}~ ${file}
fi
rm -f ${file}~
done
@TyounanMOTI
Copy link
Author

Feature request: don't touch file which isn't modified.

@KimuraShinichi
Copy link

How about this?

#!/bin/sh
# esed 's/foo/bar/g' files....
script=$1
shift 1

set CHANGED=1 # diff returns 1 for differences. (0 for no difference, 2 for errors.)
for file in $*
do
    sed -e ${script} ${file} > ${file}~
    diff ${file} ${file}~ > /dev/null
    if [ $? -eq ${CHANGED} ]; then
        cp -p ${file}~ ${file}
    fi
    rm -f ${file}~
done

@TyounanMOTI
Copy link
Author

Thank you, and it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment