Capitalising file names - is this possible with sed?
Capitalising file names - is this possible with sed?
am 31.08.2005 21:24:12 von Fred Bear
Hi
I'm just in the middle of writing a sed script to go through a number
of directories and rename a large number of files based on certain
rules I'm setting up - for example:
s/ /_ /g - to replace spaces with underscores.
However, is there a regular expression I can use to capitalise the
first letter of the filename and all those letters preceeded by a
space? So, for example:
this is my file.txt
becomes
This Is My File.txt
I'm using GNU sed under Linux so can use POSIX [:----:] notations.
Regards
Fred.
Re: Capitalising file names - is this possible with sed?
am 31.08.2005 21:31:22 von Ed Morton
Fred Bear wrote:
> Hi
>
> I'm just in the middle of writing a sed script to go through a number
> of directories and rename a large number of files based on certain
> rules I'm setting up - for example:
>
> s/ /_ /g - to replace spaces with underscores.
>
> However, is there a regular expression I can use to capitalise the
> first letter of the filename and all those letters preceeded by a
> space? So, for example:
>
> this is my file.txt
>
> becomes
>
> This Is My File.txt
>
> I'm using GNU sed under Linux so can use POSIX [:----:] notations.
>
No, but you can use GNU awk:
$ echo "this is my file.txt" |
gawk 'BEGIN{RS="[[:space:]]";FS=OFS=""}{$1=toupper($1);ORS=RT}1'
This Is My File.txt
Regards,
Ed.
Re: Capitalising file names - is this possible with sed?
am 31.08.2005 21:46:07 von Fred Bear
On Wed, 31 Aug 2005 14:31:22 -0500, Ed Morton
wrote:
>
>No, but you can use GNU awk:
>
>$ echo "this is my file.txt" |
> gawk 'BEGIN{RS="[[:space:]]";FS=OFS=""}{$1=toupper($1);ORS=RT}1'
>This Is My File.txt
>
>Regards,
>
> Ed.
Ed
Thanks for this, works perfectly.
Since I'm keeping my renaming script as compact as possible, I guess
I'll abandon sed entirely then and just use an awk script to do the
renaming - time to read the "awk" part of my O'Reilly "sed & awk" book
I guess! :-)
Fred
Re: Capitalising file names - is this possible with sed?
am 31.08.2005 21:59:56 von Ed Morton
Fred Bear wrote:
> On Wed, 31 Aug 2005 14:31:22 -0500, Ed Morton
> wrote:
>
>
>>No, but you can use GNU awk:
>>
>>$ echo "this is my file.txt" |
>> gawk 'BEGIN{RS="[[:space:]]";FS=OFS=""}{$1=toupper($1);ORS=RT}1'
>>This Is My File.txt
>>
>>Regards,
>>
>> Ed.
>
>
> Ed
>
> Thanks for this, works perfectly.
>
> Since I'm keeping my renaming script as compact as possible, I guess
> I'll abandon sed entirely then and just use an awk script to do the
> renaming - time to read the "awk" part of my O'Reilly "sed & awk" book
> I guess! :-)
You could MAYBE start there, but it's really time to get the GNU awk
book by Arnold Robbins:
Effective Awk Programming: A User's Guide for Gnu Awk, Edition 1.0.3
Regards,
Ed.
Re: Capitalising file names - is this possible with sed?
am 31.08.2005 23:06:35 von William Park
Fred Bear wrote:
> Hi
>
> I'm just in the middle of writing a sed script to go through a number
> of directories and rename a large number of files based on certain
> rules I'm setting up - for example:
>
> s/ /_ /g - to replace spaces with underscores.
>
> However, is there a regular expression I can use to capitalise the
> first letter of the filename and all those letters preceeded by a
> space? So, for example:
>
> this is my file.txt
>
> becomes
>
> This Is My File.txt
>
> I'm using GNU sed under Linux so can use POSIX [:----:] notations.
Not from 'sed'. But, in extended Bash,
a='this is my file.txt'
echo ${a|.capwords}
Similar feature exists in Python as well.
--
William Park , Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Re: Capitalising file names - is this possible with sed?
am 02.09.2005 15:26:15 von Simon Hooper
On 2005-08-31, Fred Bear wrote:
> However, is there a regular expression I can use to capitalise the
> first letter of the filename and all those letters preceeded by a
> space?
echo "$file" | sed -e 's/^./\U&/' -e 's/ [a-z]/\U&/'
\U -turn the replacement to uppercase. According to the info page, this
is a GNU Sed extension.
HTH
Simon.
Re: Capitalising file names - is this possible with sed?
am 02.09.2005 17:44:36 von Ed Morton
Simon Hooper wrote:
> On 2005-08-31, Fred Bear wrote:
>
>>However, is there a regular expression I can use to capitalise the
>>first letter of the filename and all those letters preceeded by a
>>space?
>
>
> echo "$file" | sed -e 's/^./\U&/' -e 's/ [a-z]/\U&/'
>
> \U -turn the replacement to uppercase. According to the info page, this
> is a GNU Sed extension.
I had no idea that existed - thanks for the tip. You can reduce the
above to:
sed 's/\(^\| \)./\U&/g'
but to take care of any white space preceeding each word (e.g. tabs)
rather than assuming a blank char:
sed 's/\(^\|[[:space:]]\)./\U&/g'
regards,
Ed.
Re: Capitalising file names - is this possible with sed?
am 02.09.2005 18:33:03 von Fred Bear
On Fri, 2 Sep 2005 13:26:15 +0000 (UTC), Simon Hooper
wrote:
>On 2005-08-31, Fred Bear wrote:
>> However, is there a regular expression I can use to capitalise the
>> first letter of the filename and all those letters preceeded by a
>> space?
>
>echo "$file" | sed -e 's/^./\U&/' -e 's/ [a-z]/\U&/'
>
>\U -turn the replacement to uppercase. According to the info page, this
>is a GNU Sed extension.
>
>HTH
>
>Simon.
>
Thanks, I'll try this.
Fred
Re: Capitalising file names - is this possible with sed?
am 02.09.2005 18:36:47 von James Michael Fultz
* Ed Morton :
[...]
>>>However, is there a regular expression I can use to capitalise the
>>>first letter of the filename and all those letters preceeded by a
>>>space?
[...]
>> echo "$file" | sed -e 's/^./\U&/' -e 's/ [a-z]/\U&/'
>>
>> \U -turn the replacement to uppercase. According to the info page, this
>> is a GNU Sed extension.
[...]
> but to take care of any white space preceeding each word (e.g. tabs)
> rather than assuming a blank char:
>
> sed 's/\(^\|[[:space:]]\)./\U&/g'
sed 's/\<[[:lower:]]/\U&/g'
works too
--
James Michael Fultz
Remove this part when replying ^^^^^^^^
Re: Capitalising file names - is this possible with sed?
am 02.09.2005 19:15:14 von Ed Morton
James Michael Fultz wrote:
> * Ed Morton :
> [...]
>
>>>>However, is there a regular expression I can use to capitalise the
>>>>first letter of the filename and all those letters preceeded by a
>>>>space?
>
> [...]
>
>>>echo "$file" | sed -e 's/^./\U&/' -e 's/ [a-z]/\U&/'
>>>
>>>\U -turn the replacement to uppercase. According to the info page, this
>>>is a GNU Sed extension.
>
> [...]
>
>>but to take care of any white space preceeding each word (e.g. tabs)
>>rather than assuming a blank char:
>>
>> sed 's/\(^\|[[:space:]]\)./\U&/g'
>
>
> sed 's/\<[[:lower:]]/\U&/g'
>
> works too
>
That does too much. The OP wanted to do this conversion:
$ echo "this is my file.txt" | sed 's/\(^\|[[:space:]]\)./\U&/g'
This Is My File.txt
not to also convert ".txt" to ".Txt":
$ echo "this is my file.txt" | sed 's/\<[[:lower:]]/\U&/g'
This Is My File.Txt
Regards,
Ed.
Re: Capitalising file names - is this possible with sed?
am 04.09.2005 03:41:50 von John Savage
Fred Bear writes:
>However, is there a regular expression I can use to capitalise the
>first letter of the filename and all those letters preceeded by a
>space? So, for example:
>
>this is my file.txt
>
>becomes
>
>This Is My File.txt
Even without the posix extensions it's do-able, but not exactly a one-liner:
$cat data
date & time file.pdf
global, state, locality info .doc
a b c d . x y z
X.p
$cat prog.sed
h; s/\(.\)[^ ]*[ ]*/\1/g
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
x; s/^/ /; s/ ./ /g; G
:a
/\n./{ s/\(.*\) \(.*\)\(.\)/\1_\3\2/; ba;}
s/_\(.*\)./\1/
$sed -f prog.sed data
Date_&_Time_File.pdf
Global,_State,_Locality_Info_.doc
A_B_C_D_._X_Y_Z
X.p
--
John Savage (my news address is not valid for email)
Re: Capitalising file names - is this possible with sed?
am 04.09.2005 08:16:47 von William James
Fred Bear wrote:
> Hi
>
> I'm just in the middle of writing a sed script to go through a number
> of directories and rename a large number of files based on certain
> rules I'm setting up - for example:
>
> s/ /_ /g - to replace spaces with underscores.
>
> However, is there a regular expression I can use to capitalise the
> first letter of the filename and all those letters preceeded by a
> space? So, for example:
>
> this is my file.txt
>
> becomes
>
> This Is My File.txt
>
> I'm using GNU sed under Linux so can use POSIX [:----:] notations.
echo "$file" | ruby -ne 'print split(/(\s+)/).map{|w|w.capitalize}'
Re: Capitalising file names - is this possible with sed?
am 05.09.2005 16:04:44 von Loki Harfagr
Le Fri, 02 Sep 2005 12:15:14 -0500, Ed Morton a écrit :
> James Michael Fultz wrote:
>
>> * Ed Morton :
>> [...]
>>
>>>>>However, is there a regular expression I can use to capitalise the
>>>>>first letter of the filename and all those letters preceeded by a
>>>>>space?
>>
>> [...]
>>
>>>>echo "$file" | sed -e 's/^./\U&/' -e 's/ [a-z]/\U&/'
>>>>
>>>>\U -turn the replacement to uppercase. According to the info page, this
>>>>is a GNU Sed extension.
>>
>> [...]
>>
>>>but to take care of any white space preceeding each word (e.g. tabs)
>>>rather than assuming a blank char:
>>>
>>> sed 's/\(^\|[[:space:]]\)./\U&/g'
>>
>>
>> sed 's/\<[[:lower:]]/\U&/g'
>>
>> works too
>>
>
> That does too much. The OP wanted to do this conversion:
>
> $ echo "this is my file.txt" | sed 's/\(^\|[[:space:]]\)./\U&/g'
> This Is My File.txt
>
> not to also convert ".txt" to ".Txt":
>
> $ echo "this is my file.txt" | sed 's/\<[[:lower:]]/\U&/g'
> This Is My File.Txt
This form would also care of the erroneous uppercase in words :
sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g'
example :
$ echo "this is NOT for Market.share, is it ?" | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g'
This Is Not For Market.share, Is It ?