分类: Python/Ruby
2010-10-17 00:08:11
$ echo first line |
$ echo -e "a\tb\tc\nd\te\tf" |
2. tr
synopsis
tr [OPTION] ... SET1 [SET2]
description
translate, squeeze, and/or delete characters from standard input, writing to standard output.
tr用来从标准输入中通过替换或删除操作进行字符转换。主要用于删除文件中控制字符或进行字符转换。使用tr时要转换两个字符串:字符串1用于查询,字符串2用于处理各种转换。
tr刚执行时,字符串1中的字符被映射到字符串2中的字符,然后转换操作开始.
带有最常用选项的tr命令格式为:
tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] inputfile
去除重复出现的字符
$ cat sample.txt |
$tr -s [a-z] < sample.txt |
or
$cat sample.txt | tr -s [a-z] |
删除空行
$cat space.txt |
$cat space.txt | tr -s "[\n]" |
大写到小写
除了删除控制字符,转换大小写是tr最常用的功能.为此需指定即将转换的小写字符[a-z]和转换结果[A-Z].
$echo "first second." | tr [a-z] [A-Z] |
也可以使用字符类[:lower:]和[:upper:]
$echo "first second" | tr [:lower:] [:upper:] |
删除指定字符
$cat char.txt |
输出保留字符,不要数字
$tr -cs "[a-z][A-Z]" "[\n*]" < char.txt |
转换控制字符
unix中打开来自dos的文件时,会遇到如下格式的文本.
$cat -v dos.txt |
使用-s 选项将^M,^Z转换成回车符.查ASCII表,^M是015,^Z是032.
$tr -s "[\015\032]" "\n" < dos.txt > unix.txt |
将所有Tab换空行: tab 011, space 040
$tr -s "[\011]" "[\040*]" < tabtos.txt |
[character*n]格式匹配多于一个字符
3. cut - remove sections from each line of files
cut OPTION... [FILE]...
-d, --delimiter=DELEM
-f, --fields=LIST
eg: uname -r | cut -d. -f1,2
result: $2.6
4. install - copy files and set attributes
install [OPTION]... [-T] SOURCE DEST
*install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY...
OPTION:
-[cC] -b -[dD] -[tT] -p -m ...
-d, --directory
eg: $install -d ex{1..5} <==> mkdir ex{1..5}
-D create all leading components of DEST except the last, then copy SOURCE to DEST
-m, --mode=MODE
set permission mode (as in chmod), instead of rwxr-xr-x
-p, --preserve-timestamps 保留原文件的访问修改时间
apply access/modification times of SOURCE files to orresponding destination files
eg: $install -p -m 644 sample /usr/bin
-o, --owner=OWNER
set ownership (super-user only)
-s, --strip
strip symbol tables
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-g, --group=GROUP
set group ownership, instead of process' current group
chinaunix网友2010-10-19 08:50:01
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com