您的位置:首页 > 运维架构 > Shell

Shell命令之小工具锦集(原创)

2010-05-16 16:18 176 查看
在Unix/Linux环境下工作,80%的郁闷都来自于文本处理。

毋庸置疑,Perl很牛很强大,但是,Perl太灵活了,我认可有人说的:Perl程序是那种写完一个小时之后,连自己都读不懂的程序。

来看看Shell吧,简洁就是美!

1. 替换当前目录下,所有XML后缀名文件里的某个单词

       sed -i 's/\r//g' test.txt

3. awk和sed合作

       ls >test1.txt 2> test2.txt

5. 统计行数

v=aaa;
bb="bbbbbbbbbb";
eval $v='$bb';
echo aaa:$aaa

7. 优雅的awk


8. 近乎神奇的shell变量

# % means pattern from the end, min
for x in `find ./ -name "*.txt"`;do echo $x ${x%.*};done
# %% means pattern from the end, max
for x in `find ./ -name "*.txt"`;do echo $x ${x%%.*};done
# # means pattern from the begin, min
for x in `find ./ -name "*.txt"`;do echo $x ${x#/*/};done
# ## means pattern from the begin, max
for x in `find ./ -name "*.txt"`;do echo $x ${x##/*/};done
# {#x} means the length of the variable
for x in `find ./ -name "*.txt"`;do echo $x ${#x};done
# {replace}
x="shuaigeliusiyeshuaige";y=${x/shuaige/cuonan};echo $y;
x="shuaigeliusiyeshuaige";y=${x//shuaige/cuonan};echo $y;
# {if null}
x="shuaigeliusiyeshuaige";y=${x:-notnull};echo $y;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: