LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2012, 11:54 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,164

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Translating part of a line


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.
Old 02-08-2012, 12:14 PM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
It is an escape sequence meaning upper case.

For less confusing solution:

Code:
awk 'BEGIN{ FS=""; OFS="" }{ $1=toupper($1); print $0 }' input
 
1 members found this post helpful.
Old 02-08-2012, 11:47 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
The sed code you posted will capitalize the first letter of every word, if you just need the first letter of a line, it can be simpler:
Code:
sed 's/^./\u&/' $InFile
Reference for \u
 
1 members found this post helpful.
Old 02-09-2012, 02:55 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

Quote:
Originally Posted by ntubski View Post
The sed code you posted will capitalize the first letter of every word, if you just need the first letter of a line, it can be simpler:
Code:
sed 's/^./\u&/' $InFile
Reference for \u
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.
 
2 members found this post helpful.
Old 02-09-2012, 02:27 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,164

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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 04:47 PM.
 
Old 02-11-2012, 07:07 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
bash-only solutions (version 4+), using parameter substitution:

Code:
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:

Code:
mapfile -t lines <origfile.txt
printf "%s\n" "${lines[@]^}" >tempfile.txt
mv -f tempfile.txt origfile.txt && rm -f tempfile.txt

Last edited by David the H.; 02-11-2012 at 07:11 AM. Reason: minor correction
 
3 members found this post helpful.
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed and printing only part of a line jrdioko Linux - Software 12 05-17-2012 08:15 PM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 07:35 AM
script to get a part of line from a file mystical dervish Programming 7 05-11-2007 03:57 AM
vi question regarding removing part of a line RobHill Linux - Newbie 2 03-30-2007 03:42 PM
How to extract a part of a line by sed? J_Szucs Programming 2 02-15-2003 07:49 PM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration