views:

779

answers:

5

As being quite a newbie in linux, I have the follwing question. I have list of files (this time resulting from svn status) and i want to create a script to loop them all and replace tabs with 4 spaces.

So I want from

....
D      HTML/templates/t_bla.tpl
M      HTML/templates/t_list_markt.tpl
M      HTML/templates/t_vip.tpl
M      HTML/templates/upsell.tpl
M      HTML/templates/t_warranty.tpl
M      HTML/templates/top.tpl
A  +   HTML/templates/t_r1.tpl
....

to something like

for i in <files>; expand -t4;do cp $i /tmp/x;expand -t4 /tmp/x > $i;done;

but I dont know how to do that...

+3  A: 

I would use sed, like so:

for i in files
do
    sed -i 's/\t/    /' "$i"
done

That big block in there is four spaces. ;-)

I haven't tested that, but it should work. And I'd back up your files just in case. The -i flag means that it will do the replacements on the files in-place, but if it messes up, you'll want to be able to restore them.

This assumes that $files contains the filenames. However, you can also use Adam's approach at grabbing the filenames, just use the sed command above without the "$i".

Dan Fego
thank you for your answer. But how to fill <files> in the loop??
Dr. Hfuhruhurr
Look below at Adam's answer. Essentially combine the two. His answer will give you a list of filenames to work with, then shove my sed command instead of the final "ls."
Dan Fego
+6  A: 

You can use this command:

svn st | cut -c8- | xargs ls

This will cut the first 8 characters leaving only a list of file names, without Subversion flags. You can also add grep before cut to filter only some type of changes, like /^M/. xargs will pass the list of files as arguments to a given command (ls in this case).

Adam Byrtek
thank you also for your answer, i will combine it with Dan's.
Dr. Hfuhruhurr
+2  A: 

I could not test it with real subversion output, but this should do the job:

svn st | cut -c8- | while read file; do expand -t4 $file > "$file-temp"; mv "$file-temp" "$file"; done

svn st | cut -c8- will generate a list of files without subversion flags. read will then save each entry in the variable $file and expand is used to replace the tabs with four spaces in each file.

xsl
+2  A: 

Not asking for any votes, but for the record I'll post the combined answer from @Adam Byrtek and @Dan Fego:

svn st | cut -c8- | xargs sed -i 's/\t/    /'
WMR
I will vote you up anyway :)
Adam Byrtek
A: 

Not quite what you're asking, but perhaps you should be looking into commit hooks in subversion?

You could create a hook to block check-ins of any code that contains tabs at the start of a line, or contains tabs at all.

In the repo directory on your subversion server there'll be a directory called hooks. Put something in there which is executable called 'pre-commit' and it'll be run before anything is allowed to be committed. It can return a status to block the commit if you wish.

Here's what I have to stop php files with syntax errors being checked in:

#!/bin/bash
REPOS="$1"
TXN="$2"
PHP="/usr/bin/php"

SVNLOOK=/usr/bin/svnlook

$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" > /dev/null

if [ $? -ne 0 ]
then
  echo 1>&2
  echo "You must enter a comment" 1>&2
  exit 1
fi


CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | awk '{print $2}'`

for LINE in $CHANGED
  do
  FILE=`echo $LINE | egrep \\.php$`
  if [ $? == 0 ]
    then
    MESSAGE=`$SVNLOOK cat -t "$TXN" "$REPOS" "${FILE}" | $PHP -l`
    if [ $? -ne 0 ]
      then
      echo 1>&2
      echo "***********************************" 1>&2
      echo "PHP error in: ${FILE}:" 1>&2
      echo "$MESSAGE" | sed "s| -| $FILE|g" 1>&2
      echo "***********************************" 1>&2
      exit 1
    fi
  fi
done
J.D. Fitz.Gerald