Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

wwwxxix Buzz, wwwxxxdbgf, ncurses font size, WWWXXX USABLE, WwwXXX MPEG4, WWWXXX BABBAGE WWWXXX, IIS_asps05kj, WWWXXXDOCO, WWW.XXX,GCDMO, Idl Wwwxxx

Links

XODOX
Impressum

#1: Capitalising file names - is this possible with sed?

Posted on 2005-08-31 21:24:12 by 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.

Report this message

#2: Re: Capitalising file names - is this possible with sed?

Posted on 2005-08-31 21:31:22 by 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.

Report this message

#3: Re: Capitalising file names - is this possible with sed?

Posted on 2005-08-31 21:46:07 by Fred Bear

On Wed, 31 Aug 2005 14:31:22 -0500, Ed Morton <morton@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

Report this message

#4: Re: Capitalising file names - is this possible with sed?

Posted on 2005-08-31 21:59:56 by Ed Morton

Fred Bear wrote:

> On Wed, 31 Aug 2005 14:31:22 -0500, Ed Morton <morton@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.

Report this message

#5: Re: Capitalising file names - is this possible with sed?

Posted on 2005-08-31 23:06:35 by William Park

Fred Bear <fred@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@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/

Report this message

#6: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-02 15:26:15 by Simon Hooper

On 2005-08-31, Fred Bear <fred@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.

Report this message

#7: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-02 17:44:36 by Ed Morton

Simon Hooper wrote:

> On 2005-08-31, Fred Bear <fred@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.

Report this message

#8: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-02 18:33:03 by Fred Bear

On Fri, 2 Sep 2005 13:26:15 +0000 (UTC), Simon Hooper
<trickshot@fastmail.fm> wrote:

>On 2005-08-31, Fred Bear <fred@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

Report this message

#9: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-02 18:36:47 by James Michael Fultz

* Ed Morton <morton@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@sent.as.invalid>
Remove this part when replying ^^^^^^^^

Report this message

#10: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-02 19:15:14 by Ed Morton

James Michael Fultz wrote:

> * Ed Morton <morton@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.

Report this message

#11: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-04 03:41:50 by John Savage

Fred Bear <fred@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)

Report this message

#12: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-04 08:16:47 by 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}'

Report this message

#13: Re: Capitalising file names - is this possible with sed?

Posted on 2005-09-05 16:04:44 by Loki Harfagr

Le Fri, 02 Sep 2005 12:15:14 -0500, Ed Morton a écrit :

> James Michael Fultz wrote:
>
>> * Ed Morton <morton@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 ?

Report this message