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
onkar [ So, 18 November 2007 06:07 ] [ ID #1873419 ]

Re: removing control character from all files

On Nov 18, 12:07 am, onkar <onkar.... [at] 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
govindrjujare [ So, 18 November 2007 08:40 ] [ ID #1873421 ]

Re: removing control character from all files

govindrjujare [at] gmail.com wrote:
> On Nov 18, 12:07 am, onkar <onkar.... [at] 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

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.
Cyrus Kriticos [ So, 18 November 2007 09:44 ] [ ID #1873422 ]

Re: removing control character from all files

govindrjujare [at] 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
mallin.shetland [ So, 18 November 2007 14:25 ] [ ID #1873428 ]

Re: removing control character from all files

On 2007-11-18, govindrjujare [at] gmail.com wrote:
>
>
> On Nov 18, 12:07 am, onkar <onkar.... [at] 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

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
cfajohnson [ So, 18 November 2007 15:45 ] [ ID #1873436 ]

Re: removing control character from all files

On 2007-11-18, Cyrus Kriticos wrote:
> govindrjujare [at] gmail.com wrote:
>> On Nov 18, 12:07 am, onkar <onkar.... [at] 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.
>>
>> 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"

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,

>> 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
cfajohnson [ So, 18 November 2007 15:48 ] [ ID #1873437 ]

Re: removing control character from all files

onkar <onkar.n.m [at] 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 [at] yahoo.ca>, Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
William Park [ So, 18 November 2007 22:03 ] [ ID #1873444 ]
Linux » comp.unix.shell » removing control character from all files

Vorheriges Thema: ignoring directories in find
Nächstes Thema: Unix flavor command