1830212 Members
1481 Online
109999 Solutions
New Discussion

some scripting now.

 
SOLVED
Go to solution
RAC_1
Honored Contributor

some scripting now.

I have lines as follows.

to me you
what where when

I want them to as follows.

To Me You
What Where When

I can do it with some scripting, but want a quick answer, so can not waste time on it.
There is no substitute to HARDWORK
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: some scripting now.

Hi:

# perl -lne '@list=();foreach $word (split) {push @list,ucfirst $word};print "@list"' file

Regards!

...JRF...
RAC_1
Honored Contributor

Re: some scripting now.

I was working on this--
cat test555.txt | while read line; do for i in $(echo ${line}); do first=$(echo ${i} | cut -c1|tr "[:lower:]" "[:upper:]"|tr "\n" " " ; rest=$(echo ${i} | cut -c2- | tr "\n" " "); echo ${first}${rest}; done; done

JRF's solution worked...

I must learn PERL>>>>>
There is no substitute to HARDWORK
RAC_1
Honored Contributor

Re: some scripting now.

I mentioned.
There is no substitute to HARDWORK
Rory R Hammond
Trusted Contributor

Re: some scripting now.

had to give sed exmp

mac()
{
for arg in a b c d e f g h i j k l m n o p q r s t u v w x y z
do
UP=$(echo ${arg}|tr "[a-z]" "[A-Z]" )
echo "s/ ${arg}/ ${UP}/g"
echo "s/^${arg}/${UP}/"
done
}

mac > sedfile
cat test | sed -f sedfile

Yes perl is better
There are a 100 ways to do things and 97 of them are right
Hein van den Heuvel
Honored Contributor

Re: some scripting now.


perl -pe 's/\b(\w)/uc($1)/ge' your-file

-p loop trhough input reading into and print $_ after processing
-e program text to follow
s/regexpr/expr/eg for each occurence (g) of regexpr in $_ substitute it by ethe value of expr after executing expr

\b(\w) = remember a 'word character' following an word boundary in $1
uc($1) = upcase $1

grins,
Hein.



James R. Ferguson
Acclaimed Contributor

Re: some scripting now.

Hi (again) RAC:

Hein's solution differs from mine in length but also in what constitutes a "word". It's worth noting :-))

You can test either Hein's or my solution interactively by omitting the filename argument at runtime.

The difference in input (denoted with "<" and the corresponding output (">") is thus exposed;

< this is jrf's code-running
> This Is Jrf's Code-running

< this is hein's code-running
> This Is Hein'S Code-Running

Note the treatment characters that follow a single quote (apostrophes) or a hyphen.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: some scripting now.


Good catch James.

We would need to know what defines a word for the problem to be solved.

Anyway... it was of course mostly to highlight an alternative approach.

We can make it work like Jrp's code.
Using his example using:

[root@music /tmp]$ cat x
this is jrf's code-running

[root@music /tmp]$ perl -pe 's/(^\w|\s\w)/uc($1)/ge' x
This Is Jrf's Code-running


The tricky part now is the first word which has no whitespace, and needs an 'or' construction. Using the boundary match \b avoids that.

Hein.