[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Sheflug] Apologies for long line length and a newline problem
And Lo! The Great Prophet "Chris J" uttered these words of wisdom:
>
> And Lo! The Great Prophet Lesley Anne Binks uttered these words of wisdom:
> >
> > Now in Windows 0x0D 0x0A is the carriage return line feed
> > and on Linux boxes I've always understood it's 0x0D only.
> > Emacs showed the newlines not as newlines but as ^M
> > which translates to 0x0D. Obviously this caused problems on
> > a search and replace.
>
> In ksh/sh/bash, this will process /all/ files in the current directory.
>
> for i in *
> do
.
.
.
As an extension, as they're files from a Win32 platform. Do any of the
filenames have spaces in? If so, you'll need to change the 'for i in...' to
this instead:
ls | while read i
do
if [ -f "$i" ]; then
mv "$i" /tmp/workfile
tr -d '\r' < /tmp/workfile > "$i"
fi
done
Note the pipe to while and that $i is in "double quotes".
Standard UNIX will tokenise the file at spaces (so a file called "This Is
A File.txt" is seen as four entities, which generally doesn't work. Another
way of getting round this is to fiddle with the IFS environment variable,
something like this (untested):
oldIFS=$IFS
IFS=<get a newline character here>
for i in *
do
.
.
.
done
IFS=$oldIFS
And finally (in lessons of shell-token parsing): recent shells expand '*'
without the problem of spaces, so the original for loop would work (however
"for i in `ls`" wouldn't). You will still need to double quote the variable
though within the loop to ensure the filename gets treated as a single
entity within it.
In short, the most portable is the ls pipe to while -- this should work
regardless of ksh/sh/bash shell version. If anyone is a csh, tcsh or zsh
user then you'll have to consult your manpages.
HTH,
Chris...
--
\ Chris Johnson \ NP:
\ cej [at] nightwolf.org.uk \
\ http://cej.nightwolf.org.uk/ \
\ http://redclaw.org.uk/ ~---------------------------------------
___________________________________________________________________
Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html
GNU the choice of a complete generation.