Operating System - Linux
1753767 Members
5870 Online
108799 Solutions
New Discussion юеВ

Shell script question using sed or awk

 
SOLVED
Go to solution
Unix or Linux?
Frequent Advisor

Shell script question using sed or awk

How do i convert individual characters to upper case and also remove redundant characters at the same time. My data is the following

john.doe


How do i convert it to:

John Doe
13 REPLIES 13
Ivan Ferreira
Honored Contributor

Re: Shell script question using sed or awk

Try this:

echo john.doe |tr [:lower:] [:upper:] |tr . " "
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Unix or Linux?
Frequent Advisor

Re: Shell script question using sed or awk

I think that will convert everything to upper case whereas I need specific characters converted. I need the first character and the character after the full stop.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell script question using sed or awk

Hi:

# echo "john doe"|perl -ne '@a=split;foreach (@a) {push @b,ucfirst};print "@b\n"'

Regards!

...JRF...

Unix or Linux?
Frequent Advisor

Re: Shell script question using sed or awk

Great thank you, it worked. Is there any way that this can be done with awk or sed?
Ivan Ferreira
Honored Contributor

Re: Shell script question using sed or awk

Try with this:

#!/usr/bin/awk -f
# convert upper case letters to lower case
BEGIN {
SEPARATOR_CHAR=".";
LC="abcdefghijklmnopqrstuvwxyz";
UC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
{
out="";
separator=0;
# look at each character
for(i=1;i<=length($0);i++) {
# get the character to be checked
char=substr($0,i,1);
j=index(LC,char);
# is it the first letter?
if (( i == 1 ) || ( separator == 1 )) {
# print (char);
out = out substr(UC,j,1);
separator = 0;
}
else if (char == SEPARATOR_CHAR ) {
char=" ";
out = out char
separator=1
} else {
out = out char;
}

}
printf("%s\n", out);
}
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Shell script question using sed or awk

Hi (again):

With 'awk' the problem becomes more tedious since you would have to isolate the first character of each "word" (say with 'substr'); convert it to uppercase with 'toupper'; and then convert the remaining characters of that word to lowercase with 'tolower'. Perl offers far, far many more features than does 'awk'.

Regards!

...JRF...
Unix or Linux?
Frequent Advisor

Re: Shell script question using sed or awk

ok but the problem is I dont understand the statement

echo "john doe"|perl -ne '@a=split;foreach (@a) {push @b,ucfirst};print "@b\n"'



Could you give an explanation of the statement.
Thank you
James R. Ferguson
Acclaimed Contributor

Re: Shell script question using sed or awk

Hi (again):

# echo "john doe"|perl -ne '@a=split;foreach (@a) {push @b,ucfirst};print "@b\n"'

The perl switch '-n' says create a read loop. For every line read, split on whitespace (blanks, or tabs) the line's elements into a list called "a". Now, walk each element of list "a", and uppercase the first character of each element. As you do this, "push" (store) the result into a new list called "b". When every element in a line has been handled, print the modified line.

You could use this with a file, like:

# perl -ne '@a=split;foreach (@a) {push @b,ucfirst};print "@b\n"' yourfile

Regards!

...JRF...
Unix or Linux?
Frequent Advisor

Re: Shell script question using sed or awk

Great but it doesnt seem to append to a file. All the other statments do append to a file. My code is as follows:

> testFile
printf "\n" >> testFile
echo "john.doe"|perl -ne '@a=split;foreach (@a) {push @b,ucfirst};print "@b\n\n"' >> testFile
ps -U john.doe >> testFile


Its only the echo line that is not appending to testFile. Any ideas why?