高级Bash脚本编程指南(26):文本处理命令(二)
成于坚持,败于止步
处理文本和文本文件的命令
look
look命令与grep命令很相似, 但是这个命令只能做"字典查询", 也就是它所搜索的文件必须是已经排过序的单词列表. 默认情况下, 如果没有指定搜索哪个文件, look命令就默认搜索/usr/share/dict/words, 当然也可以指定其他目录下的文件进行搜索.
一个实例:检查列表中单词的正确性
#!/bin/bash
# 对指定数据文件中的每个单词都做一遍字典查询.
name="./file" # 指定的要搜索的数据文件.
echo
# Stephane Chazelas建议使用下边更简洁的方法:
while read word && [[ $word != end ]]
do if look "$word" > /dev/null
then echo "\"$word\" is valid."
else echo "\"$word\" is invalid."
fi
done <"$name"
exit 0
# will never be called
while [ "$word" != end ] # 数据文件中最后一个单词.
do
read word # 从数据文件中读, 因为在循环的后边重定向了.
look $word > /dev/null # 不想将字典文件中的行显示出来.
lookup=$? # 'look'命令的退出状态.
if [ "$lookup" -eq 0 ]
then
echo "\"$word\" is valid."
else
echo "\"$word\" is invalid."
fi
done <"$name" # 将stdin重定向到$file, 所以"reads"来自于$file.
echo
exit 0
结果:
root@ubuntu:~/resource/shell-study/0620-2013# cat file
this
good
example
then
echo
23
45
root@ubuntu:~/resource/shell-study/0620-2013# ./test7.sh
"this" is valid.
"good" is valid.
"example" is valid.
"then" is valid.
"echo" is valid.
"23" is invalid.
"45" is invalid.
root@ubuntu:~/resource/shell-study/0620-2013#
上面实例的第二种方法还存在点问题没有解决
sed, awk
这个两个命令都是独立的脚本语言, 尤其适合分析文本文件和命令输出. 既可以单独使用, 也可以结合管道和在shell脚本中使用.
sed
非交互式的"流编辑器", 在批处理模式下, 允许使用多个ex命令. 你会发现它在shell脚本中非常有用.
awk
可编程的文件提取器和文件格式化工具, 在结构化的文本文件中, 处理或提取特定域(特定列)具有非常好的表现. 它的语法与C语言很类似.
你会经常跟上面这两个命令打交道的,他们可不那么好相处,O(∩_∩)O~我感觉是O(∩_∩)O~
wc
wc可以统计文件或I/O流中的"单词数量":
root@ubuntu:~/resource/shell-study/0620-2013# cat file
this
good
example
then
echo
23
45
root@ubuntu:~/resource/shell-study/0620-2013# wc file
7 7 34 file
root@ubuntu:~/resource/shell-study/0620-2013#
第一个7代表7 lines,第二个7代表7 words,第三个34代码34 characters,你可以扒开手指头去数数了,O(∩_∩)O~,最后是文件名
wc -w 统计单词数量.
root@ubuntu:~/resource/shell-study/0620-2013# wc -w file
7 file
root@ubuntu:~/resource/shell-study/0620-2013#
wc -l 统计行数量.
root@ubuntu:~/resource/shell-study/0620-2013# wc -l file
7 file
root@ubuntu:~/resource/shell-study/0620-2013#
wc -c 统计字节数量.
root@ubuntu:~/resource/shell-study/0620-2013# wc -c file
34 file
root@ubuntu:~/resource/shell-study/0620-2013#
wc -m 统计字符数量.
root@ubuntu:~/resource/shell-study/0620-2013# wc -m file
34 file
root@ubuntu:~/resource/shell-study/0620-2013#
wc -L 给出文件中最长行的长度.
root@ubuntu:~/resource/shell-study/0620-2013# wc -L file
7 file
root@ubuntu:~/resource/shell-study/0620-2013#
一个实例:使用wc命令来统计当前工作目录下有多少个脚本文件:
root@ubuntu:~/resource/shell-study/0620-2013# ls
file file2 test1.sh test3.sh test5.sh test7.sh
file1 sys.log test2.sh test4.sh test6.sh
root@ubuntu:~/resource/shell-study/0620-2013# ls *.sh | wc -l
7
root@ubuntu:~/resource/shell-study/0620-2013#
接着看一个实例:wc命令来统计所有以 t 开头的文件的大小.
root@ubuntu:~/resource/shell-study/0620-2013# ls
file file2 test1.sh test3.sh test5.sh test7.sh
file1 sys.log test2.sh test4.sh test6.sh
root@ubuntu:~/resource/shell-study/0620-2013# wc t* | grep total |awk '{print $3}'
5295
root@ubuntu:~/resource/shell-study/0620-2013# wc t*
12 19 148 test1.sh
20 54 511 test2.sh
81 189 2535 test3.sh
15 41 391 test4.sh
17 40 335 test5.sh
24 58 521 test6.sh
36 93 854 test7.sh
205 494 5295 total
root@ubuntu:~/resource/shell-study/0620-2013#
使用wc命令来查看指定文件中包含"某内容"的行一共有多少
root@ubuntu:~/resource/shell-study/0620-2013# cat file
this my dog
good luch
this is mine
hello
happy
this ok
that this
root@ubuntu:~/resource/shell-study/0620-2013# grep "this" file | wc -l
4
root@ubuntu:~/resource/shell-study/0620-2013#
先到这里,今天休息一下,O(∩_∩)O~
先到这里了,O(∩_∩)O~
我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html
待续。。。。。。