LC to UC selectively
am 07.04.2008 21:16:28 von RickM
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
Re: LC to UC selectively
am 07.04.2008 22:53:54 von Johann Kappacher
rickm@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
Re: LC to UC selectively
am 07.04.2008 22:56:34 von Johann Kappacher
Johann Kappacher wrote:
> rickm@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.
Re: LC to UC selectively
am 07.04.2008 23:27:35 von Ed Morton
On 4/7/2008 2:16 PM, rickm@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.
Re: LC to UC selectively
am 07.04.2008 23:33:11 von Johann Kappacher
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
Re: LC to UC selectively
am 07.04.2008 23:52:57 von Johann Kappacher
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 ...
Re: LC to UC selectively
am 07.04.2008 23:59:02 von Ed Morton
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.
Re: LC to UC selectively
am 08.04.2008 04:07:48 von Barry Margolin
In article <47FA9926.6060209@lsupcaemnt.com>,
Ed Morton 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@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
Re: LC to UC selectively
am 08.04.2008 04:15:17 von Ed Morton
On 4/7/2008 9:07 PM, Barry Margolin wrote:
> In article <47FA9926.6060209@lsupcaemnt.com>,
> Ed Morton 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.
Re: LC to UC selectively
am 08.04.2008 10:04:25 von PK
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.
Re: LC to UC selectively
am 08.04.2008 10:10:36 von PK
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.
Re: LC to UC selectively
am 08.04.2008 13:59:36 von Ed Morton
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.
Re: LC to UC selectively
am 08.04.2008 16:12:15 von gazelle
In article <47FB5E28.1040305@lsupcaemnt.com>,
Ed Morton 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.