Sed
From Koset Surakomol
Contents |
Change a string throughout a file
cat index.htm | sed -e s/\/body/\/BODY/g index.htm.new
Multiple files
for i in * ; do sed s/exp1/exp2/ $i $i-new mv $i-new $i; done
or this ...
$ find . -type f | xargs grep -l 'foo' | xargs sed -i '' -e 's/foo/bar/g'
Insert text blob
E.g. inserting google analytics script into html files (UNTESTED)
for x in `ls *html`; do sed /s\/body/r google $x new/$x done
Replace a space in a filename with an underscore
THIS script is the one that works the best. Original [1]
#!/bin/ksh find `pwd` -name "* *"|while read file do target=`echo "$file"|tr -s ' '|tr ' ' '-'` mv "$file" "$target" done
The following are kept for archival purposes.
for i in *
do
OLDNAME=$i
NEWNAME=`echo $i | tr ' ' '_' | tr A-Z a-z | sed s/_-_/-/g`
if [ $NEWNAME != $OLDNAME ]
then
TMPNAME=$i_TMP
echo
mv -v -- $OLDNAME $TMPNAME
mv -v -- $TMPNAME $NEWNAME
fi
if [ -d $NEWNAME ]
then
echo Recursing lowercase for directory $NEWNAME
$0 $NEWNAME
fi
done
or this
#!/bin/bash
ls | while read -r FILE
do
mv -v $FILE `echo $FILE | tr ' ' '_' | tr -d '[{}(),\!]' | tr -d \' | tr '[A-Z]' '[a-z]' | sed 's/_-_/_/g'`
done
