Linux中转换字符串大小写的姿势

Table of Contents

在数据处理中肯定会遇到大小写的转换,下面说说Linux中转换字符串大小写的几种方式。

1  AWK

awk是强大的报告生成器,其很多内置函数很有用,比如tolower和toupper就是处理大小写的,用命令说话,如下,

PS:gawk才支持大小写的转换函数tolower和toupper。

2  tr

tr是专门处理字符串的神器,话不多说,如下图,

3 tr

tr命令还有一种方法转换大小写。

3  sed

sed作为古老的流编辑器,仍然是一把文本处理的利剑。

将每个单词的首字母变为大写,如图,

将所有小写转换为大写,如图,

将所有大写转换为小写,如图,

 

关于sed的这个用法我还是头一次看见,有兴趣的朋友可以在shell下运行info sed查看解释,大致如下,

 

3.5 The `s' Command
===================

The syntax of the `s' (as in substitute) command is
`s/REGEXP/REPLACEMENT/FLAGS'.  The `/' characters may be uniformly
replaced by any other single character within any given `s' command.
The `/' character (or whatever other character is used in its stead)
can appear in the REGEXP or REPLACEMENT only if it is preceded by a `\'
character.

   The `s' command is probably the most important in `sed' and has a
lot of different options.  Its basic concept is simple: the `s' command
attempts to match the pattern space against the supplied REGEXP; if the
match is successful, then that portion of the pattern space which was
matched is replaced with REPLACEMENT.

   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,

`\E'
     Stop case conversion started by `\L' or `\U'.

   To include a literal `\', `&', or newline in the final replacement,
be sure to precede the desired `\', `&', or newline in the REPLACEMENT
with a `\'.