sed: capitalizing the first letter of a line
Hello guys,
I'd like to instruct sed to capitalize the first letter of every line. I've gotten this far:
Code:
$ echo "the first word is not capitalized." | sed 's/^t/T/'
The first word is not capitalized.
That seems to work (yay!). Say I'd like to do it for multiple different lines of text, then of course I could do something like this:
Code:
$ cat bigstory.txt | sed 's/^a/A/; s/^b/B/; s/^c/C/; s/^d/D/; s/^e/E/'
etcetera, and make a huge command. But that is not very elegant. There has to be a nice way to do it. Do you know one?
Re: sed: capitalizing the first letter of a line
I know I'm not supposed to say this but... Google is your friend, really.
Code:
sed -e 's/^\(.\)/\U\1/g' foo.txt
This seems to be non-standard, I do not know the standard way.
Re: sed: capitalizing the first letter of a line
There's a special instruction \u for that - combined with the & which substitutes the whole pattern found
Code:
$ echo "the first word is not capitalized." | sed 's/.*/\u&/'
The first word is not capitalized.
FYI, to capitalize the whole thing it's \U
Code:
$ echo "the first word is not capitalized." | sed 's/.*/\U&/'
THE FIRST WORD IS NOT CAPITALIZED.
Re: sed: capitalizing the first letter of a line
Thanks guys. I'm learning how to use the command line, this helps. sed seems to be a difficult command to work with, the way it takes arguments appears so random.
Quote:
Originally Posted by
Bachstelze
I know I'm not supposed to say this but... Google is your friend, really.
I'll try to do that more in the future.
Re: sed: capitalizing the first letter of a line
sed is not just a "command", for most intents and purposes you can treat it as a programming language in its own right.
Re: sed: capitalizing the first letter of a line
Quote:
Originally Posted by
Bachstelze
I know I'm not supposed to say this but... Google is your friend, really.
Code:
sed -e 's/^\(.\)/\U\1/g' foo.txt
This seems to be non-standard, I do not know the standard way.
Why do you say it's non-standard? And why have g when it's just the first occurrence in a line that needs to be changed which seems to be taken care of by ^?
Re: sed: capitalizing the first letter of a line
Quote:
Originally Posted by
vasa1
Why do you say it's non-standard? And why have g when it's just the first occurrence in a line that needs to be changed which seems to be taken care of by ^?
I say it is non-standard because it does not work on non-GNU versions of sed. I use /g because I am not very good at sed. :)
Re: sed: capitalizing the first letter of a line
Quote:
Originally Posted by
Bachstelze
I say it is non-standard because it does not work on non-GNU versions of sed. ...
Thanks, I didn't know that :)
Re: sed: capitalizing the first letter of a line
Well look at that. I ask a dumb question an now we've all learned something new. You're welcome. Hehe. :biggrin:
In the main time, I found this website that is very useful for sed: http://www.grymoire.com/Unix/Sed.html