Context Sensitive smallcap => TitleCap conversion in an entire text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Context Sensitive smallcap => TitleCap conversion in an entire text file
# 1  
Old 01-15-2011
Power Context Sensitive smallcap => TitleCap conversion in an entire text file

I desperately need to write a script that go into text documents that list the location of files in small caps and have it convert the directories and subdirectories that lead to the file as Title Caps while leaving the file itself in small caps... to illustrate what I mean:

I need to turn txt file upon txt files containing:
-----------------------------------------------------------------
\music\artist\album\title-in-small-caps.mp3
......
\music\arc-en-ciel\jam sessions\12 alec alec boum!.mp3
\music\babel soundtrack\babel original soundtrack\10 bibo no aozora_endless flight_bab.mp3
\music\benjamin zephaniah\belly of de beast\wake up.mp3
-----------------------------------------------

into:
------------------------------------------------
\Music\Artist\Album\title-in-small-caps.mp3
...........
\Music\Arc-En-Ciel\Jam Sessions\12 alec alec boum!.mp3
\Music\Babel Soundtrack\Babel Original Soundtrack\10 bibo no aozora_endless flight_bab.mp3
\Music\Benjamin Zephaniah\Belly Of De Beast\wake up.mp3
---------------------------

I can think up the algorithm easily but I have zero knowledge of the synthax need to pull this off:

Algoritm:
for each line, count the number of "/" characters there are, if a string is between two "/" characters, TitleCase it. If the string is between a "/" character and a ".mp3" string, do not change it and move on to next line.

how would I go about doing this in a script or pearl?

This thread discusses capitalization but it will capitalize everything... [see trackback link]


Does anyone have any ideas?
# 2  
Old 01-15-2011
What Operating System and version are you running?
What Shell you you use (e.g. bash, ksh, real Bourne, whatever)?
Do you use a UK or American character set, or a character set from some other Country? It matters because in alternative character sets the range "a-z" can either include characters we don't want to change or miss out characters which we do want to change.

How many directories? This is a question about whether efficiency matters.
To count the directories:
Code:
find . -type d -print | wc -l

Do you have any existing directories which are in the desired format? This a very important question because we do not want to overwrite existing directories.


Footnote. The directory format in your examples uses the character "\" to delimit directories. This is a feature of M$ Operating Systems such as Windows, and not a feature of unix Operating Systems which use the character "/". What is the exact format of your directoy names on your disc?

Last edited by methyl; 01-15-2011 at 09:00 PM.. Reason: various typos, and footnote
# 3  
Old 01-16-2011
@methyl brings up some very good points, which may affect how this PERL script:
Code:
$, = '\\';

while (<>) {
    @F = split /\\/;
    $f = pop @F;
    print(( map { ucfirst($_) } @F ), $f);
}

works on your system with your data.
# 4  
Old 01-17-2011
Thank you for your help friends.

I am dealing here wih Directories of my music library. All the directories are already named in the correct format (they are Title Case). What I am acutally trying to modify is not the directories themselves but multiple m3u playlists written by a windows program where case is irrelivant in address where the directories are listed in lower case.

Usually modifying the case of text can be accomplished in any word processor but the tricky part is that although directories are in Title Case on the hard drive, the actual mp3 files they contain are in lower case. So when altering my playlists, the directories need to be listed in Title Case whereas the mp3s need to remain unchanged (in lower case). I have thousands of lines to modify so, I'm trying to see if there is a script that can do this without having me go at it one by one.

I use an american charset on my Linux. I am on Kubuntu 10.04 running KDE. I'm using Bash (the console program itself is called Konsole). I altering an m3u text file writen by an american windows based program called DoubleTwist and this is part of my effort to leave this program and migrate all my music data to SongBird. I hope to make a tutorial for other people wanting to leave this program as well.
# 5  
Old 01-17-2011
Try something like...
Code:
$ cat a1.awk
BEGIN{FS=OFS="\\"}
{
   for(i=1;i<NF;i++)
      $i=titlecase($i)
   print $0
}

$ cat file1.txt
\music\artist\album\title-in-small-caps.mp3
......
\music\arc-en-ciel\jam sessions\12 alec alec boum!.mp3
\music\babel soundtrack\babel original soundtrack\10 bibo no aozora_endless flight_bab.mp3
\music\benjamin zephaniah\belly of de beast\wake up.mp3

$ gawk -f titlecase.awk -f a1.awk file1.txt > file2.txt

$ cat file2.txt
\Music\Artist\Album\title-in-small-caps.mp3
......
\Music\Arc-En-Ciel\Jam Sessions\12 alec alec boum!.mp3
\Music\Babel Soundtrack\Babel Original Soundtrack\10 bibo no aozora_endless flight_bab.mp3
\Music\Benjamin Zephaniah\Belly of De Beast\wake up.mp3

$

You would have to download the titlecase function from http://www.pement.org/awk/titlecase.awk.txt
# 6  
Old 01-17-2011
Code:
$ ruby -F'\\'  -ane '$F[1..-2].map{|x| x.gsub!(/\w+/) {|y| y.capitalize} };puts $F.join("\\")' file 
\Music\Artist\Album\title-in-small-caps.mp3
\Music\Arc-En-Ciel\Jam Sessions\12 alec alec boum!.mp3
\Music\Babel Soundtrack\Babel Original Soundtrack\10 bibo no aozora_endless flight_bab.mp3
\Music\Benjamin Zephaniah\Belly Of De Beast\wake up.mp3

# 7  
Old 01-17-2011
Thanks I'll give it a whirl...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print text between 2 strings for the entire file

hey guys, for the following output: starting open open close close starting close starting open close close starting open open close open (2 Replies)
Discussion started by: boaz733
2 Replies

2. UNIX for Dummies Questions & Answers

Fill csv entire column with content from another text file

I have a csv that looks like this: ,yude-to-nap2,0,0,0,0,0 ,2twis-yude-to-nap2,0,0,0,0,0 ,2tiws-yude-to-nap2,0,0,0,0,0 ,2arcos-yude-to-nap2,0,0,0,0,0 and another file named m1 that has a single line of text as content: Feb 1 15:30:20 How can I fill the whole the empty column of the... (1 Reply)
Discussion started by: RobertoRivera
1 Replies

3. Shell Programming and Scripting

HTML Conversion of text file

Hi, I have following text file. I want to convert it into the below HTML format. Kindly help. Input Text File Header 1 ======= Name:*** Age:*** Address:*** Work Phone:*** Email:*** Mobile:*** Country:*** City:*** Pincode:*** some text here **** (10 Replies)
Discussion started by: ctrld
10 Replies

4. Shell Programming and Scripting

Conversion of a text file to html

hi i hav a file called focus. which is the output file. i want to convert this file to html file and mail it. help with code and explanatio. i am basic in unix (7 Replies)
Discussion started by: wasim999
7 Replies

5. Shell Programming and Scripting

Conversion of spaces Text file into CSV format file

Input file (each line is separaed by spaces )given below: Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- -----... (18 Replies)
Discussion started by: sreenath1037
18 Replies

6. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

7. Shell Programming and Scripting

Transpose an entire text file

Hello all, I want to transpose the rows of a file to the columns (every characters include spaces), i.e.: input: abcdefg 123 456 output: a1 b2 c3 d e4 f5 g6 I wrote a script: #!/bin/csh -f (15 Replies)
Discussion started by: heavenfish
15 Replies

8. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

9. UNIX for Dummies Questions & Answers

Binary data to text file conversion

Dear Sir; i want to know how the binary data convert to text file or readablw format (ASCII).If possible pl. help me for the software and where it is available for download. i.e. (1 Reply)
Discussion started by: auro123
1 Replies

10. UNIX for Dummies Questions & Answers

ISAM FILE CONVERSION TO TEXT

I am having a problem. There is one ISAM file available in SCO UNIX environment. I want to convert it to simple comma delimited text file. Actual problem is with the size of the file. For the time being, size is 1.3 GB and I have to grab the subset of the data after every five minutes e.g. in... (3 Replies)
Discussion started by: a.waqar
3 Replies
Login or Register to Ask a Question