Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265239
  • 博文数量: 74
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 793
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-25 21:01
文章分类

全部博文(74)

文章存档

2011年(1)

2010年(32)

2009年(32)

2008年(9)

我的朋友

分类: LINUX

2009-10-06 22:16:08

  • grep
usage of -v -i -l -r -c:
 -v, --invert-match
              Invert  the  sense of matching, to select non-matching lines.  (-v is specified by POSIX.)
-i, --ignore-case
              Ignore case distinctions in  both  the  PATTERN  and  the  input  files.   (-i  is specified by POSIX.)
-l, --files-with-matches
              Suppress normal output; instead print the name  of  each  input  file  from  which output  would  normally  have  been  printed.  The scanning will stop on the first match.  (-l is specified by POSIX.)
-c, --count
              Suppress  normal  output;  instead  print a count of matching lines for each input file.  With the -v, --invert-match option (see below), count  non-matching  lines. (-c is specified by POSIX.)
-R, -r, --recursive
              Read all files under each directory, recursively; this is equivalent to the -d recurse option.

# grep -v John /etc/passwd
jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash
# grep -c John /etc/passwd
2
# grep -cv John /etc/passwd
39
# grep -i john /etc/passwd
jsmith:x:1082:1082:John Smith:/home/jsmith:/bin/bash
jdoe:x:1083:1083:John Doe:/home/jdoe:/bin/bash
# grep -ri john /home/users
/home/users/subdir1/letter.txt:John, Thanks for your contribution.
/home/users/name_list.txt:John Smith
/home/users/name_list.txt:John Doe
# grep -ril john /root
/home/users/subdir1/letter.txt
/home/users/name_list.txt


Checking for full words, not for sub-strings using grep -w

If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out all the lines.Or you can use the \ pattern.

$ grep -w in aaaaaa.sh

select i in mon tue wed exit

case $i in

$ grep "\" aaaaaa.sh

select i in mon tue wed exit

case $i in


Displaying lines before/after/around the match using grep -A, -B and -C

When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match.

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD


$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD


Highlighting the search using GREP_OPTIONS

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='101;9'


you can add it to ~/.bashrc and reload it,so next time you will see the highlight word when you use grep

display the lines which matches all the given pattern using -e

$ grep -e case -e esac aaaaaa.sh
    case $i in
    esac


  • join

$ cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
$ cat bonus.txt
100 $5,000
200 $500
300 $3,000
400 $1,250
$ join employee.txt bonus.txt
100 Jason Smith $5,000
200 John Doe $500
300 Sanjay Gupta $3,000
400 Ashok Sharma $1,250


  • sort

$ sort -t: -k 3n /etc/passwd (sort -t: -n -k3 /etc/passwd)
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.100.101 dev-db.thegeekstuff.com dev-db
192.168.100.102 prod-db.thegeekstuff.com prod-db
192.168.101.20 dev-web.thegeekstuff.com dev-web
192.168.101.21 prod-web.thegeekstuff.com prod-web


  • uniq
Uniq command is mostly used in combination with sort command, as uniq removes duplicates only from a sorted file. i.e In order for uniq to work, all the duplicate entries should be in the adjacent lines. Following are some common examples.

$ sort namesd.txt | uniq = sort –u namesd.txt


$sort namesd.txt | uniq –c
2 Alex Jason:200:Sales
2 Emma Thomas:100:Marketing
1 Madison Randy:300:Product Development
1 Nisha Singh:500:Sales
1 Sanjay Gupta:400:Support
$ sort namesd.txt | uniq –cd
2 Alex Jason:200:Sales
2 Emma Thomas:100:Marketing


  • find

find files that are not modified in the last 60 days
# find . -mtime +60
find files that are modified in the last 60 days
# find . -mtime -60
# find / -type f -size +100M (greater that 100M)
# find / -type f -size -100M (less than 100M)


  • xargs
 The -l and -i options appear in the 1997 version of the POSIX standard, but do not appear in the 2004 version of the standard.  Therefore you should use -L and -I instead, respectively.
  • cut
  • stat
  • diff
  • comm
  • tr
reference:
阅读(746) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~