Shell/Perl script to convert to Capitalize case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell/Perl script to convert to Capitalize case
# 8  
Old 01-07-2009
There's got to be a better approach ...
I'm still looking for it Smilie
# 9  
Old 01-07-2009
Quote:
Originally Posted by parshant_bvcoe
I need a shell script which will convert the given string within a <title> tag to Capitalize case.

E.g "<title>hi man: check this out</title>"

to "<title>Hi Man: Check This Out</title>"

With two shell functions...
Code:
_capword()
{
  case $1 in
    a*) _CAPWORD=A${1#?};; b*) _CAPWORD=B${1#?};;
    c*) _CAPWORD=C${1#?};; d*) _CAPWORD=D${1#?};;
    e*) _CAPWORD=E${1#?};; f*) _CAPWORD=F${1#?};;
    g*) _CAPWORD=G${1#?};; h*) _CAPWORD=H${1#?};;
    i*) _CAPWORD=I${1#?};; j*) _CAPWORD=J${1#?};;
    k*) _CAPWORD=K${1#?};; l*) _CAPWORD=L${1#?};;
    m*) _CAPWORD=M${1#?};; n*) _CAPWORD=N${1#?};;
    o*) _CAPWORD=O${1#?};; p*) _CAPWORD=P${1#?};;
    q*) _CAPWORD=Q${1#?};; r*) _CAPWORD=R${1#?};;
    s*) _CAPWORD=S${1#?};; t*) _CAPWORD=T${1#?};;
    u*) _CAPWORD=U${1#?};; v*) _CAPWORD=V${1#?};;
    w*) _CAPWORD=W${1#?};; x*) _CAPWORD=X${1#?};;
    y*) _CAPWORD=Y${1#?};; z*) _CAPWORD=Z${1#?};;
    *)  _CAPWORD=$1 ;;
  esac
}

_cap_phrase()
{
   _PHRASE="$* "
   _CAP_PHRASE=
   while [ -n "$_PHRASE" ]
   do
     _TEMP=${_PHRASE#* }
     _capword "${_PHRASE%"$_TEMP"}"
     _CAP_PHRASE="$_CAP_PHRASE$_CAPWORD"
     _PHRASE=$_TEMP
   done
   _CAP_PHRASE=${_CAP_PHRASE% }
}

...the script is simple:

Code:
title="<title>hi man: check this out</title>"

## Strip HTML tags
title=${title#*>}
title="${title%<*}"

_cap_phrase "$title"
captitle="<title>${_CAP_PHRASE% }</title>"

printf "%s\n" "$captitle"


Last edited by cfajohnson; 01-07-2009 at 09:13 PM..
# 10  
Old 01-07-2009
Quote:
Originally Posted by radoulov
There's got to be a better approach ...
I'm still looking for it Smilie
Maybe something like this, but I don't have a shell to try it, so untested...
Code:
perl -pi -e 's/([^<\/])\b(\w)/$1\U$2/g' filename

# 11  
Old 01-08-2009
Quote:
Originally Posted by reborg
Maybe something like this, but I don't have a shell to try it, so untested...
Code:
perl -pi -e 's/([^<\/])\b(\w)/$1\U$2/g' filename

The problem is that the above command will capitalize the words between all type of tags, not only for <title></title>.
Consider the following:

Code:
% print '<head><script>this not</script><title>this yes</title></head>'|
perl -pe's/([^<\/])\b(\w)/$1\U$2/g' 
<head><script>This Not</script><title>This Yes</title></head>

% print '<head><script>this not</script><title>this yes</title></head>'|
perl -pe's|
pipe quote>     (?<=<title>)
pipe quote>     (.*?)
pipe quote>     (?=</title>)
pipe quote>     |($x=$1)=~s/\b(\w)/\u$1/g;$x
pipe quote>     |gxe'
<head><script>this not</script><title>This Yes</title></head>

# 12  
Old 01-08-2009
Code:
#! /usr/bin/perl
open FH,"Leo.pm";
while(<FH>){
	s/(\w+)/ucfirst($1)/eg;
	s/<(.*?)>/'<'.lcfirst($1).'>'/e;
	s/\/(.*)>$/lcfirst($1).'>'/e;
	print;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Shell script to convert words to Title case

Hi :) I have a .txt file with thousands of words. I was wondering if i could use a simple sed or awk command to convert / replace all words in the text file to Title Case format ? Example: from: this is line one this is line two this is line three to desired output: This Is Line... (8 Replies)
Discussion started by: martinsmith
8 Replies

2. Shell Programming and Scripting

Convert shell script to Perl

Hello,,I have a very small script that contains these lines; and it works perfectly; however I need to use Perl now as I will need to feel variables from a MySQL table into this; to it would be nice to start by converting this first... find / -perm 777 \( -type f -o -type d \) -exec ls -lid {}... (1 Reply)
Discussion started by: gvolpini
1 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Help: how to convert perl script to shell script

bash$ mdb-schema x.mdb | perl -wpe 's%^DROP TABLE %DROP TABLE IF EXISTS %i; s%(Memo/Hyperlink|DateTime( \(Short\))?)%TEXT%i; s%(Boolean|Byte|Byte|Numeric|Replication ID|(\w+ )?Integer)%INTEGER%i; s%(BINARY|OLE|Unknown (+)?)%BLOB%i; s%\s*\(\d+\)\s*(,?*)$%${1}%;' | sqlite3 x.db ... (1 Reply)
Discussion started by: jackpapa
1 Replies

5. Shell Programming and Scripting

convert perl code to shell script

This is about how to Monitoring folder for new files using shell script im doing a project using smsserver tools 3. i have used a perl script to handle incoming messages. the content of each message must be directed to a java program. this program generates the answer to reply to the user... (2 Replies)
Discussion started by: x34
2 Replies

6. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

7. Shell Programming and Scripting

convert PERL script to SHELL script

Hi all experts, I am new to Unix. Could you please help me to convert the following PERL script to SHELL? I will appreciate. #!/bin/sh use strict; use warnings; my $robot_num = 0; my %rob_tapes; foreach (`/usr/openv/volmgr/bin/vmquery -l -rn $robot_num`) { $rob_tapes{(split)} = 1; }... (6 Replies)
Discussion started by: ma466
6 Replies

8. Shell Programming and Scripting

convert the below perl sript to shell script

perl script: my $logdir = '/smp/dyn/logfiles/fsm/mp/mp'; $logdir = $logdir ."/mp${toDate}*"; i tried to make it..as below .. but not working .. date +%m%d%y logdir = /smp/dyn/logfiles/fsm/mp/mp logdir=$logdir/mp"$date" but it was not working..... can someone please help me out in... (1 Reply)
Discussion started by: mail2sant
1 Replies

9. UNIX for Advanced & Expert Users

Shell script to convert to Title case

I need a shell script which will convert the given string to Title case. E.g "hi man" to "Hi man" (5 Replies)
Discussion started by: SankarV
5 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question