|
Home > Archive > Unix Shell > November 2007 > removing control character from all files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
removing control character from all files
|
|
|
| I have directory structure containing many files. The directory
structure is of depth 10. I want to remove the control character from
all the files in the directory structure . If the control character is
^M please help me with the command to recursively visit the files in
the directory structure and replace the control character.
Thanks & regards,
Onkar
| |
| govindrjujare@gmail.com 2007-11-18, 7:44 am |
| On Nov 18, 12:07 am, onkar <onkar....@gmail.com> wrote:
> I have directory structure containing many files. The directory
> structure is of depth 10. I want to remove the control character from
> all the files in the directory structure . If the control character is
> ^M please help me with the command to recursively visit the files in
> the directory structure and replace the control character.
>
> Thanks & regards,
> Onkar
Follg gives the basic idea. Add error handling/exceptions as
required. Type the ^M as a sequence of Ctrl-V and Ctrl-M (in
vi editor). If you want to overwrite the files, first write the output
to a temp file and then copy over.
find . -type f | while read name
do
cat $name | sed -e 's/^M//g' > $name.clean
done
| |
| Cyrus Kriticos 2007-11-18, 7:44 am |
| govindrjujare@gmail.com wrote:
> On Nov 18, 12:07 am, onkar <onkar....@gmail.com> wrote:
>
> Follg gives the basic idea. Add error handling/exceptions as
> required. Type the ^M as a sequence of Ctrl-V and Ctrl-M (in
> vi editor). If you want to overwrite the files, first write the output
> to a temp file and then copy over.
>
> find . -type f | while read name
> do
> cat $name | sed -e 's/^M//g' > $name.clean
With GNU sed you can edit files in place:
sed -i -e 's/foo/bar/g' "$name"
> done
--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
| |
| mallin.shetland 2007-11-18, 7:45 am |
| govindrjujare@gmail.com scrisse:
> ...
> find . -type f | while read name
> do
> cat $name | sed -e 's/^M//g' > $name.clean
> done
You like complex thinks.
With GNU sed:
find . -type f -execdir sed -i 's/\r//g' {} \;
This avoids trouble if you have foo & foo.clean
| |
| Chris F.A. Johnson 2007-11-18, 1:28 pm |
| On 2007-11-18, govindrjujare@gmail.com wrote:
>
>
> On Nov 18, 12:07 am, onkar <onkar....@gmail.com> wrote:
>
> Follg gives the basic idea. Add error handling/exceptions as
> required. Type the ^M as a sequence of Ctrl-V and Ctrl-M (in
> vi editor). If you want to overwrite the files, first write the output
> to a temp file and then copy over.
>
> find . -type f | while read name
To keep leading or trailing whitespace in filenames, set IFS, and
to keep backslashes, use the -r option:
find . -type f | while IFS= read -r name
> do
> cat $name | sed -e 's/^M//g' > $name.clean
You don't need cat, and the variables should be quoted in case
there are spaces in the filenames:
sed -e 's/^M//g' "$name" > "$name.clean"
> done
>
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Chris F.A. Johnson 2007-11-18, 1:28 pm |
| On 2007-11-18, Cyrus Kriticos wrote:
> govindrjujare@gmail.com wrote:
>
> With GNU sed you can edit files in place:
>
> sed -i -e 's/foo/bar/g' "$name"
If you are going to use the -i option, also give a suffix so that
a backup will be made:
sed -i.bak -e 's/foo/bar/g' "$name"
FreeBSD sed also has the -i option, and the suffix is mandatory,
[vbcol=seagreen]
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| William Park 2007-11-18, 7:22 pm |
| onkar <onkar.n.m@gmail.com> wrote:
> I have directory structure containing many files. The directory
> structure is of depth 10. I want to remove the control character from
> all the files in the directory structure . If the control character is
> ^M please help me with the command to recursively visit the files in
> the directory structure and replace the control character.
>
>
> Thanks & regards,
> Onkar
man tr
--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
|
|
|
|
|