removing control character from all files

removing control character from all files

am 18.11.2007 06:07:29 von onkar

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

Re: removing control character from all files

am 18.11.2007 08:40:04 von govindrjujare

On Nov 18, 12:07 am, onkar 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

Re: removing control character from all files

am 18.11.2007 09:44:53 von Cyrus Kriticos

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

Re: removing control character from all files

am 18.11.2007 14:25:54 von mallin.shetland

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

Re: removing control character from all files

am 18.11.2007 15:45:22 von cfajohnson

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

Re: removing control character from all files

am 18.11.2007 15:48:12 von cfajohnson

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

Re: removing control character from all files

am 18.11.2007 22:03:16 von William Park

onkar 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 , Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/