Capitalising file names - is this possible with sed?
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?
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?
On Wed, 31 Aug 2005 14:31:22 -0500, Ed Morton <morton [at] lsupcaemnt.com>
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?
Fred Bear wrote:
> On Wed, 31 Aug 2005 14:31:22 -0500, Ed Morton <morton [at] lsupcaemnt.com>
> 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?
Fred Bear <fred [at] bearcave.com> 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 <opengeometry [at] yahoo.ca>, 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?
On 2005-08-31, Fred Bear <fred [at] bearcave.com> 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?
Simon Hooper wrote:
> On 2005-08-31, Fred Bear <fred [at] bearcave.com> 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?
On Fri, 2 Sep 2005 13:26:15 +0000 (UTC), Simon Hooper
<trickshot [at] fastmail.fm> wrote:
>On 2005-08-31, Fred Bear <fred [at] bearcave.com> 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?
* Ed Morton <morton [at] lsupcaemnt.com>:
[...]
>>>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 <xyzzy [at] sent.as.invalid>
Remove this part when replying ^^^^^^^^
Re: Capitalising file names - is this possible with sed?
James Michael Fultz wrote:
> * Ed Morton <morton [at] lsupcaemnt.com>:
> [...]
>
>>>>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?
Fred Bear <fred [at] bearcave.com> 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?
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?
Le Fri, 02 Sep 2005 12:15:14 -0500, Ed Morton a écrit :
> James Michael Fultz wrote:
>
>> * Ed Morton <morton [at] lsupcaemnt.com>:
>> [...]
>>
>>>>>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 ?
Linux » comp.unix.shell » Capitalising file names - is this possible with sed?