187 lines
5.7 KiB
HTML
187 lines
5.7 KiB
HTML
<!--startcut ======================================================= -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<html>
|
|
<head>
|
|
<META NAME="generator" CONTENT="lgazmail v1.3E.q">
|
|
<TITLE>The Answer Gang 64: Question in shell programming</TITLE>
|
|
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
|
|
LINK="#3366FF" VLINK="#A000A0">
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<P> <hr>
|
|
<CENTER>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<!-- *** END navbar *** -->
|
|
</CENTER>
|
|
</p>
|
|
<P> <hr> <P>
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<center>
|
|
<H1><A NAME="answer">
|
|
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
|
|
border="0" align="middle">
|
|
<font color="#B03060">The Answer Gang</font>
|
|
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
|
|
border="0" align="middle">
|
|
</A></H1>
|
|
<BR>
|
|
<H4>By Jim Dennis, Ben Okopnik, Dan Wilder,
|
|
the Editors of Linux Gazette...
|
|
and You!
|
|
<br>Send questions (or interesting answers) to
|
|
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a>
|
|
</H4>
|
|
</center>
|
|
|
|
<p><hr><p>
|
|
<!-- endcut ======================================================= -->
|
|
<!-- begin 25 -->
|
|
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
|
|
height="50" width="60" alt="(?) " border="0"
|
|
>Question in shell programming</H3>
|
|
|
|
|
|
<p><strong>From Jane Liu
|
|
</strong></p>
|
|
<p align="right"><strong>Answered By Dan Wilder, Jim Dennis
|
|
<br></strong></p>
|
|
|
|
<!-- sig -->
|
|
|
|
<P><STRONG>
|
|
I wanted to change the first letter of each line to upper case? How can I do it with awk?
|
|
</STRONG></P>
|
|
<P><STRONG>
|
|
Thanks,
|
|
Jane
|
|
</STRONG></P>
|
|
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
|
|
HEIGHT="28" WIDTH="50" BORDER="0"
|
|
> [Dan]
|
|
Here's one way:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><pre>
|
|
#!/usr/bin/awk -f
|
|
|
|
{
|
|
a = substr($0, 1, 1)
|
|
b = toupper(a)
|
|
sub(a, b, $0)
|
|
print
|
|
}
|
|
</pre></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
"a" has the length-1 substring of the current input record, $0,
|
|
beginning at the first character of the input line.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
"b" has the upper-case equivalent of "a".
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
"sub" substitutes what's in "b" in place of the first
|
|
match in the string for what's in "a". The third argument
|
|
to "sub" may be left implicit, but I prefer not to do so,
|
|
feeling that putting $0 in makes the operation of the program
|
|
more obvious at a glance.
|
|
</BLOCKQUOTE>
|
|
|
|
<!-- sig -->
|
|
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
|
|
HEIGHT="28" WIDTH="50" BORDER="0"
|
|
> [JimD]
|
|
Here's a simple one liner:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
awk '{ sub(/^./,toupper(substr($0,1,1))); print; }'
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... substitute the first character of the implicit pattern space
|
|
($0) with the value returned by the <TT>"toupper()</TT>" function, when
|
|
it's called on the substring of $0 (the default pattern space) from
|
|
the first character and continuing for one character.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
Then print the thing.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
Can't get much simpler than that.
|
|
</BLOCKQUOTE>
|
|
|
|
<p><strong><IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
|
|
HEIGHT="28" WIDTH="50" BORDER="0"
|
|
>
|
|
Thanks a lot! It works perfectly.
|
|
</strong></p>
|
|
<strong></p>
|
|
Thanks again!
|
|
<br>Jane
|
|
</strong></p>
|
|
|
|
<!-- sig -->
|
|
|
|
|
|
<!-- end 25 -->
|
|
<!--startcut ======================================================= -->
|
|
<P> <hr> </p>
|
|
<!-- *** BEGIN copyright *** -->
|
|
<H5 align="center">This page edited and maintained by the Editors
|
|
of <I>Linux Gazette</I>
|
|
<a href="http://www.linuxgazette.com/copying.html"
|
|
>Copyright ©</a> 2001
|
|
<BR>Published in issue 64 of <I>Linux Gazette</I> March 2001</H5>
|
|
<H6 ALIGN="center">HTML script maintained by
|
|
<A HREF="mailto:star@starshine.org">Heather Stern</a> of
|
|
Starshine Technical Services,
|
|
<A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
|
|
</H6>
|
|
<!-- *** END copyright *** -->
|
|
<P> <hr>
|
|
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<p align="center">
|
|
<table width="100%" border="0"><tr>
|
|
<td align="right" valign="center" rowspan="3"
|
|
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
|
|
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0">
|
|
<A HREF="../lg_answer64.html"
|
|
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
|
|
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
|
|
<td align="center" valign="center"><A HREF="../lg_answer64.html#greeting"><img align="middle"
|
|
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A>
|
|
<A HREF="1.html">1</A>
|
|
<A HREF="2.html">2</A>
|
|
<A HREF="3.html">3</A>
|
|
<A HREF="4.html">4</A>
|
|
<A HREF="6.html">6</A>
|
|
<A HREF="7.html">7</A>
|
|
<A HREF="9.html">9</A></td>
|
|
<td align="left" valign="center" rowspan="3"><A HREF="../../tag/kb.html"
|
|
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
|
|
ALT="[ Index of Past Answers ]" border="0"></A>
|
|
<IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
|
|
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr>
|
|
<tr>
|
|
<td align="center" valign="center"><A HREF="11.html">11</A>
|
|
<A HREF="12.html">12</A>
|
|
<A HREF="15.html">15</A>
|
|
<A HREF="16.html">16</A>
|
|
<A HREF="18.html">18</A></td>
|
|
</tr><tr>
|
|
<td align="center" valign="center"><A HREF="20.html">20</A>
|
|
<A HREF="22.html">22</A>
|
|
<A HREF="24.html">24</A>
|
|
<A HREF="25.html">25</A>
|
|
<A HREF="26.html">26</A>
|
|
<A HREF="28.html">28</A>
|
|
<A HREF="29.html">29</A></td>
|
|
</tr></table>
|
|
</p>
|
|
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<P> <hr>
|
|
<CENTER>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<!-- *** END navbar *** -->
|
|
</CENTER>
|
|
</p>
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
</BODY></HTML>
|
|
<!--endcut ========================================================= -->
|