ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I want to change the first character of each line to upper case.
Sample input:
Quote:
tom
dick
harry
Desired output:
Quote:
Tom
Dick
Harry
The brute force technique uses cut to make two work files, tr one of them to upper case, and paste to arrive at the desired result. Googling in search of a more direct method I came upon this one:
Code:
sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g' $InFile
It works but I don't understand it. Attempting to simplify this one-liner I tried this:
Code:
sed 's/\([a-z]\)\(.*\)/\u\1\2/' $InFile
This also works but why?!? The magic may lie in the \u but what does that mean?
Daniel B. Martin
Click here to see the post LQ members have rated as the most helpful post in this thread.
Even if you want to capitalize the first letter of every word, it can be simpler
Code:
$ echo abba dav | sed 's/\b./\u&/g'
Abba Dav
# add brackets to show what matched
$ echo abba dav | sed 's/\b./[\u&]/g'
[A]bba[ ][D]av
This work because non-alphabetic characters (particularly space) can not be capitalized.
Similarly, you can capitalize last character of each word
Code:
$ echo abba dav | sed 's/.\b/[\u&]/g'
abb[A][ ]da[V]
Excerpt from `info sed escapes':
Quote:
`\b'
Matches a word boundary; that is it matches if the character to
the left is a "word" character and the character to the right is a
"non-word" character, or vice-versa.
`\B'
Matches everywhere but on a word boundary; that is it matches if
the character to the left and the character to the right are
either both "word" characters or both "non-word" characters.
Note that these escape sequences as well as case conversion ones (\u etc) are all GNU extensions.
Thank you, H_TexMex_H, ntubski, and firstfire for useful contributions to this thread. Double thanks to ntubski for providing a pointer to the place where I can learn more on this subject.
I am in awe of the amazing range of operations of which sed is capable.
We can mark this thread SOLVED!
Daniel B. Martin
Last edited by danielbmartin; 02-09-2012 at 03:47 PM.
while read line; do
echo "${line^}" >>tempfile.txt
done <origfile.txt
[[ -n "$line" ]] && echo "${line^}" >>tempfile.txt #processes the final line if there's no ending newline in the text
mv -f tempfile.txt origfile.txt && rm -f tempfile.txt
Or store the text in an array first, with mapfile:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.