LC to UC selectively

I have a file that looks like this:

poly poly
metal1 metal1
metal2 metal2
metal3 metal3

but need to be able to change one column to uppercase so either:

POLY poly
RickM [ Mo, 07 April 2008 21:16 ] [ ID #1936905 ]

Re: LC to UC selectively

rickm [at] galaxy.nsc.com wrote:
> I have a file that looks like this:
>
> poly poly
> metal1 metal1
> metal2 metal2
> metal3 metal3
>
> but need to be able to change one column to uppercase so either:
>
> POLY poly
>
I would try:

typeset -u UC

while read UC LC
do
echo "$UC \t $LC"
done < input_file
Johann Kappacher [ Mo, 07 April 2008 22:53 ] [ ID #1936907 ]

Re: LC to UC selectively

Johann Kappacher wrote:
> rickm [at] galaxy.nsc.com wrote:
>> I have a file that looks like this:
>>
>> poly poly
>> metal1 metal1
>> metal2 metal2
>> metal3 metal3
>>
>> but need to be able to change one column to uppercase so either:
>>
>> POLY poly
>>
> I would try:
>
> typeset -u UC
>
> while read UC LC
> do
> echo "$UC \t $LC"
> done < input_file
>
Perhaps, you will need to change the IFS (Input Field Separator) first.
Please, check man page or tutorial for bash or ksh.
Johann Kappacher [ Mo, 07 April 2008 22:56 ] [ ID #1936908 ]

Re: LC to UC selectively

On 4/7/2008 2:16 PM, rickm [at] galaxy.nsc.com wrote:
> I have a file that looks like this:
>
> poly poly
> metal1 metal1
> metal2 metal2
> metal3 metal3
>
> but need to be able to change one column to uppercase so either:
>
> POLY poly
>

This may be what you want if you don't care about preserving the white-space:

awk '{$1=toupper($1)}1' file

If you do care, tell us what that white space is.

Ed.
Ed Morton [ Mo, 07 April 2008 23:27 ] [ ID #1936912 ]

Re: LC to UC selectively

Ed Morton wrote:

> awk '{$1=toupper($1)}1' file
Nice solution, not strict shell, but very nice!

btw: what stands the 3rd "1" for?

--jk
Johann Kappacher [ Mo, 07 April 2008 23:33 ] [ ID #1936913 ]

Re: LC to UC selectively

Johann Kappacher wrote:
> Ed Morton wrote:
>
>> awk '{$1=toupper($1)}1' file
> Nice solution, not strict shell, but very nice!
>
> btw: what stands the 3rd "1" for?
Brrr, shame on me, this is true Pidgin English!

I would prefer a perlish approach, but this is comp.unix.shell ...
Johann Kappacher [ Mo, 07 April 2008 23:52 ] [ ID #1936914 ]

Re: LC to UC selectively

On 4/7/2008 4:33 PM, Johann Kappacher wrote:
> Ed Morton wrote:
>
>
>> awk '{$1=toupper($1)}1' file
>
> Nice solution, not strict shell, but very nice!

People who post questions here are rarely looking for solutions that only
involve shell builtins (if that's what you mean by "strict shell") but instead
are happy with any tools called from the shell (tr, grep, sed, awk, etc...).

> btw: what stands the 3rd "1" for?

It's a true condition which invokes the default action of printing the current
record.

Ed.
Ed Morton [ Mo, 07 April 2008 23:59 ] [ ID #1936915 ]

Re: LC to UC selectively

In article <47FA9926.6060209 [at] lsupcaemnt.com>,
Ed Morton <morton [at] lsupcaemnt.com> wrote:

> On 4/7/2008 4:33 PM, Johann Kappacher wrote:
> > Ed Morton wrote:
> >
> >
> >> awk '{$1=toupper($1)}1' file
> >
> > Nice solution, not strict shell, but very nice!
>
> People who post questions here are rarely looking for solutions that only
> involve shell builtins (if that's what you mean by "strict shell") but instead
> are happy with any tools called from the shell (tr, grep, sed, awk, etc...).
>
> > btw: what stands the 3rd "1" for?
>
> It's a true condition which invokes the default action of printing the current
> record.

I.e. a more terse, but less clear, way of writing

awk '{$1=toupper($1); print}' file

--
Barry Margolin, barmar [at] alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
Barry Margolin [ Di, 08 April 2008 04:07 ] [ ID #1937597 ]

Re: LC to UC selectively

On 4/7/2008 9:07 PM, Barry Margolin wrote:
> In article <47FA9926.6060209 [at] lsupcaemnt.com>,
> Ed Morton <morton [at] lsupcaemnt.com> wrote:
>
>
>>On 4/7/2008 4:33 PM, Johann Kappacher wrote:
>>
>>>Ed Morton wrote:
>>>
>>>
>>>
>>>> awk '{$1=toupper($1)}1' file
>>>
>>>Nice solution, not strict shell, but very nice!
>>
>>People who post questions here are rarely looking for solutions that only
>>involve shell builtins (if that's what you mean by "strict shell") but instead
>>are happy with any tools called from the shell (tr, grep, sed, awk, etc...).
>>
>>
>>>btw: what stands the 3rd "1" for?
>>
>>It's a true condition which invokes the default action of printing the current
>>record.
>
>
> I.e. a more terse, but less clear, way of writing
>
> awk '{$1=toupper($1); print}' file
>

which is itself a more terse, but less clear, way of writing

awk '{$1=toupper($1); print $0}' file

we could go on adding language constructs, but using "1" that way is a very
common awk idiom so it's worth getting used to.

Ed.
Ed Morton [ Di, 08 April 2008 04:15 ] [ ID #1937598 ]

Re: LC to UC selectively

Ed Morton wrote:

> which is itself a more terse, but less clear, way of writing
>
> awk '{$1=toupper($1); print $0}' file
>
> we could go on adding language constructs, but using "1" that way is a
> very common awk idiom so it's worth getting used to.

This should work as well, right? It seems to work for me.

awk '$1=toupper($1)' file

Thanks

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
PK [ Di, 08 April 2008 10:04 ] [ ID #1937602 ]

Re: LC to UC selectively

pk wrote:

> This should work as well, right? It seems to work for me.
>
> awk '$1=toupper($1)' file

Ok, it there are no empty lines, of course.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
PK [ Di, 08 April 2008 10:10 ] [ ID #1937603 ]

Re: LC to UC selectively

On 4/8/2008 3:10 AM, pk wrote:
> pk wrote:
>
>
>>This should work as well, right? It seems to work for me.
>>
>>awk '$1=toupper($1)' file
>
>
> Ok, it there are no empty lines, of course.
>

Right. That's the caveat.

Ed.
Ed Morton [ Di, 08 April 2008 13:59 ] [ ID #1937610 ]

Re: LC to UC selectively

In article <47FB5E28.1040305 [at] lsupcaemnt.com>,
Ed Morton <morton [at] lsupcaemnt.com> wrote:
>
>
>On 4/8/2008 3:10 AM, pk wrote:
>> pk wrote:
>>
>>
>>>This should work as well, right? It seems to work for me.
>>>
>>>awk '$1=toupper($1)' file
>>
>>
>> Ok, it there are no empty lines, of course.
>>
>
>Right. That's the caveat.
>
> Ed.
>

Or if you don't care about the blank lines. Which, for me, is often the
case. If the file comes from less reliable sources, those less reliable
sources often put blank lines in the files they send me, for no good reason.
gazelle [ Di, 08 April 2008 16:12 ] [ ID #1937616 ]
Linux » comp.unix.shell » LC to UC selectively

Vorheriges Thema: Mounting a hard drive from another computer
Nächstes Thema: cp -pd question