search and replace in files

For all files under app/views, replace all tabls with two spaces.
1
find app/views -type f -print0 | xargs -0 sed -i 's/\t/ /g'

broken down:

1
find app/views -type f

'find' lists all the files (with relative path) at or below the specified path. 'type -f' gives you 'files' ie not directories. It also exclude symlinks, etc. [ see list of file types ]

'-print 0' is a hack for cross-system compatibility issues. [ see documentation on -print option
File-Name ]

1
xargs -0 ... 

xargs simply takes whatever list is passed to it and executes an executable you specify once for each line it receives. the -0 tells xargs to handle the result of find -print0

1
sed -i

the -i argument means to edit the file inline... ie overwrite it rather than writing the contents to another file.
"Danger Will Robinson! " ... this will rewrite your files ... if you screw this up and aren't using a repository, this could be a disaster for you.

's/needle/replacement/g'

this is the same substitution format as found in vim. replace regex at 'needle' with string 'replacement'. /s is substitute, g is global, ie not just the first occurrence.

Post a comment


(lesstile enabled - surround code blocks with ---)