LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A "sed" Question... (https://www.linuxquestions.org/questions/programming-9/a-sed-question-733267/)

Ronzalez 06-16-2009 03:04 AM

A "sed" Question...
 
I'm taking a Linux class, and we're all now wrestling with the "sed" editor. One of the substitution commands we were given contains this:

sed 's/^\(.\)/\U\1' > name

So far, I understand everything up to the part in bold red. What does the "\U" part mean?

...Also, this doesn't pertain to a test or homework question. It's just something we were shown in class, and I still don't understand it.

ghostdog74 06-16-2009 03:20 AM

you should also learn to read the man page. type man sed, or info sed , \U is documented.

colucix 06-16-2009 03:28 AM

Try this:
Code:

echo gooFy | sed 's/^\(.\)/\U\1/'
and this:
Code:

echo GooFy | sed 's/^\(.\)/\L\1/'
Is it a bit more clear, now? :)

Tinkster 06-16-2009 03:34 AM

Quote:

Originally Posted by ghostdog74 (Post 3575568)
you should also learn to read the man page. type man sed, or info sed , \U is documented.


info-pages and their navigation suck, and the man
page doesn't have that info ;}


Cheers,
Tink

pixellany 06-16-2009 06:41 AM

This does not appear to be documented at the SED oracle: http://www.grymoire.com/Unix/Sed.html

Where has someone actually seen it?

colucix 06-16-2009 06:49 AM

Quote:

Originally Posted by pixellany (Post 3575732)
This does not appear to be documented at the SED oracle: http://www.grymoire.com/Unix/Sed.html

Where has someone actually seen it?

From GNU sed official documentation: http://www.gnu.org/software/sed/manu...s_0022-Command
Code:

Finally, as a GNU sed extension, you can include a special sequence
made of a backslash and one of the letters L, l, U, u, or E. The meaning
is as follows:

\L
    Turn the replacement to lowercase until a \U or \E is found,
\l
    Turn the next character to lowercase,
\U
    Turn the replacement to uppercase until a \L or \E is found,
\u
    Turn the next character to uppercase,
\E
    Stop case conversion started by \L or \U.


scottro11 06-16-2009 06:52 AM

Most Linux documentation is pretty poor. On CentOS, man sed doesn't list \U.

However, googling

sed "\U" gets it on the third hit. (Note that the \U was in quotes).

Quote:


Caseinsensitive Diff

Use the following Line-Matching-Preprocessor-Command to convert all input to uppercase:
sed 's/\(.*\)/\U\1/'
Here the ".*" is a regular expression that matches any string and in this context matches all characters in the line. The "\1" in the replacement string refers to the matched text within the first pair of "\(" and "\)". The "\U" converts the inserted text to uppercase.


sycamorex 06-16-2009 07:17 AM

A much more cumbersome way of doing it would be:

Quote:

sed 'y/abcdefghijklmnoprstuwxyz/ABCDEFGHIJKLMNOPRSTUWXYZ/' file
:)

ghostdog74 06-16-2009 07:19 AM

Quote:

Originally Posted by Tinkster (Post 3575586)
info-pages and their navigation suck, and the man
page doesn't have that info ;}


Cheers,
Tink

info pages are alright. to search for \U, just key in "/" (it will prompt for search word), then type \U.
extract of info sed
Code:

...
  The REPLACEMENT can contain `\N' (N being a number from 1 to 9,
inclusive) references, which refer to the portion of the match which is
contained between the Nth `\(' and its matching `\)'.  Also, the
REPLACEMENT can contain unescaped `&' characters which reference the
whole matched portion of the pattern space.  Finally, as a GNU `sed'
extension, you can include a special sequence made of a backslash and
one of the letters `L', `l', `U', `u', or `E'.  The meaning is as
follows:

`\L'
    Turn the replacement to lowercase until a `\U' or `\E' is found,

`\l'
    Turn the next character to lowercase,

`\U'
    Turn the replacement to uppercase until a `\L' or `\E' is found,

`\u'
    Turn the next character to uppercase,

.....


Ronzalez 06-17-2009 01:14 PM

Thanks
 
Okay, that makes sense -- thanks, everybody. I understand it now. It's basically substituting the first letter for its capitalized version, and then storing the word in a register.

It's all just a bit awkward for me at this point.


All times are GMT -5. The time now is 07:21 PM.