posts - 297,  comments - 15,  trackbacks - 0
1.关于文件对比的Scripts
diff -b -r $Dir1 $Dir2 | grep -v "Common subdirectories" | grep -e "^diff" -e "^Binary files" -e "^Only"
diff:
 -b  --ignore-space-change 
              Ignore changes in the amount of white space.
 -r  --recursive
              Recursively compare any subdirectories found.
grep:
 -v, --invert-match
              Invert the sense of matching, to select non-matching lines.
 -e PATTERN, --regexp=PATTERN
              Use PATTERN as the pattern; useful to protect patterns beginning with -.

2. grep的正则表达式:
比如,要匹配两个单詞:  word  exec
grep -E "word|execl"  是可以得到正确结果的
AND:
grep -E "word&execl"  和预期的不一样
grep -E 'excel.*word|work.*excel'是可以的。
注:正则表达式跟逻辑操作符是俩概念 , 只有awk支持||,&&这样的操作,
用grep的话, 也只支持|而不支持&。
正则基础:)
$cat rfile
abcd
abc
ab
$grep -E 'abc.*' rfile
abcd
abc
$grep -E 'abc*' rfile
abcd
abc
ab

3. 如何获得一个文件的前N列或是其中的几列?
cut -f-2 -d ' ' filename
-f field   -d dilimeter

4.删除某个目录下不是以.sh .h .c结尾的文件
find . -type f | awk '!/\.sh$/ && !/\.[c|h]$/{print $1}' | xargs rm

5.原来用冒号分隔的,选出几列来,改成用竖杠分隔的
cat /etc/passwd | awk -F: '{print $1"|"$2"|"$3"|"$4}'

6.去掉文件中的字符,比如去掉从win上传到Linux上的文件时会有个^M,若去掉他可以用:
tr -d '\015'  < filename

7.目录树是
A/B/C/
A/B/C/ccc/...
A/B/C/bbb/...
A/B/C/D/aaa/...
我现在在A下,我要查找html后缀文件, 但是不想它在包含aaa||bbb||ccc的目录下查找.
find . \( -name Input -o -name Output -o -name Current \) -type d -prune -o -name "*.htm" -print
这个命令中的目录排除没有起作用,为什么嗯?很困惑。
find . -wholename './A/B' -prune -o -print
这个命令也不起作用,更困惑了,不认wholename

8. 循环读文件的命令
for x in `seq 1 12`; do cat $x|awk '{print $9}' && sleep 3;done
seq 是Linux 中一個預設的外部命令,一般用作一堆數字的簡化寫法,如
seq 1 10
便會出現
1
2
3
4
5
6
7
8
9
10
它還有三個選項
  -f, --format=FORMAT      use printf style floating-point FORMAT (default: %g)
  -s, --separator=STRING   use STRING to separate numbers (default: \n)
  -w, --equal-width        equalize width by padding with leading zeroes
-f 最常用 , 例如一次制做 10 個名 dir001 , dir002 .. dir010 的目錄,它便很有用途,我們可以這樣
下一個命令便可了
seq -f 'dir%03g' 1 10 | xargs mkdir

mkdir $(seq -f 'dir%03g' 1 10)
它用的是 printf 的格式 , %03g' 代表以三位浮點數,以此方法,
如用bash3 的 printf也可作為等價命令
printf 'dir%03d\n' {1..10} | xargs mkdir  或 mkdir `printf 'dir%03d ' {1..10}`
awk 當然也可以
awk 'BEGIN { while (num < 10 ) printf "dir%03d\n", ++num ; exit}' | xargs mkdir
這樣會比寫一個腳本快, 不必寫成
for dir in 001 002 003 004 005 006 007 008 009 010
do
    mkdir dir${dir}
done

9.找出当前目录下大于100M的目录
find . -maxdepth 1 -type d | xargs du -sh | grep -v '\.$'| grep -P '^\S+M\t' | awk -FM '$1>1'

posted on 2009-11-08 11:19 chatler 阅读(229) 评论(0)  编辑 收藏 引用 所属分类: Shell

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜