2014-07-20 23:21:17 doupei2006 阅读数 725

grep的用法 

grep ‘abc’   grep ‘abc’ | grep ‘def’grep ‘abc’ | grep -v ‘def’    -v 是不包含

grep -A3 ‘xxx’ 包含前三行
grep -B3 ‘xxx’ 包含后三行
grep -C3 ‘xxx’ 包含前后三行
grep -i  不区分大小写
grep -n 显示匹配的行
grep -L 显示匹配的文件


find命令

find pathname  -options [-print -exec -ok…]
find . -name ‘*.php’ -print   可以用正则表达
find . -perm 777 -print   打印当前目录权限是777的文件


IO重定向

echo ‘’ >abc.php  可以清空文件
echo ‘acb’ > abc.php 覆盖文件内容

echo ‘acb’ >> abc.php 追加到文件末尾

利用 >>重定向可以合并多个文件


统计文件大小

du -b 统计某个文件或目录的字节数

du -k 统计某个文件或目录的字节数单位是KB

du -m 统计某个文件或目录的字节数单位是MB

注:

ls -l 命令, wc命名也能统计字节数量



2016-07-19 18:39:28 stuShan 阅读数 1579

查找匹配的行并且显示行号

ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'init' -n flaskr.py
38:def init_db():
46:@app.cli.command('initdb')
47:def initdb_command():
49:    init_db()
ld@ubuntu:/mnt/hgfs/haShare/test$ cat -n flaskr.py # 检验一下
....

在多个文件中查找

ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'content' file*.txt
file1.txt:content of file 1
file1.txt:content of file 1
file1.txt:content of file 1
file1.txt:content of file 1
file2.txt:content of file 2
file2.txt:content of file 2
file2.txt:content of file 2
file2.txt:content of file 2
file3.txt:content of file 3
file3.txt:content of file 3
file3.txt:content of file 3
file3.txt:content of file 3
ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*.txt # 检验
content of file 1
content of file 1
content of file 1
content of file 1
content of file 2
content of file 2
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
content of file 3

只显示不匹配的行的内容

修改文件内容 在 file1.txt 中加入 hehe da

ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'content' -v file*.txt
file1.txt:hehe da
ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*.txt
content of file 1
content of file 1
content of file 1
content of file 1
hehe da
content of file 2
content of file 2
content of file 2
content of file 2
content of file 3
content of file 3
content of file 3
content of file 3

统计匹配的行数

ld@ubuntu:/mnt/hgfs/haShare/test$ cat file*.txt
content of file 1
hehe da
content of file 2
content of file 2

content of file 3
content of file 3
content of file 3

ld@ubuntu:/mnt/hgfs/haShare/test$ grep 'content' -c file*.txt
file1.txt:1
file2.txt:2
file3.txt:3
ld@ubuntu:/mnt/hgfs/haShare/test$

打印匹配项数

ld@ubuntu:/mnt/hgfs/haShare/test$ echo -e "1 2 3 4 5 \n hello \n 5 6" | grep -o "[0-9]" | wc -l
7
ld@ubuntu:/mnt/hgfs/haShare/test$ echo -e "1 2 3 4 5 \n hello \n 5 6" | grep -o "[0-9]"
1
2
3
4
5
5
6

使用正则表达式匹配,同时只输出匹配部分

ld@ubuntu:/mnt/hgfs/haShare/test$ echo 'xixi: hello world!!!' | grep -o -E "\w+"
xixi
hello
world
ld@ubuntu:/mnt/hgfs/haShare/test$ echo 'xixi: hello world!!!' | grep -o -E "\w."
xi
xi
he
ll
o
wo
rl
d!

打印匹配样式的字符偏移

ld@ubuntu:/mnt/hgfs/haShare/test$ echo gun is not unix | grep -b -o 'not'
7:not
ld@ubuntu:/mnt/hgfs/haShare/test$ echo gun is not unix | grep -b -o 'xixi'

搜索多个文件并找出匹配文本位于哪一个文件中

ld@ubuntu:/mnt/hgfs/haShare/test$ grep -l init_db *
flaskr.py
grepNote
grep: tow: Is a directory # 因为是个目录所以暂时搞不了 -R 可以帮忙
ld@ubuntu:/mnt/hgfs/haShare/test$

返回不匹配的文件列表

ld@ubuntu:/mnt/hgfs/haShare/test$ grep -L init_db *
file1.txt
file2.txt
file3.txt
multi_blanks
note
setup.py
grep: tow: Is a directory
tow

递归查找查找某个文本在哪个文件中的哪一行

# 在windows 下面装了 cmder才能这样使用这样使用 shell命令
C:\Users\shan\Documents\Atom
λ grep -R -n 'grep 过滤数据'
JSON对象遍历.md:22:grep 过滤数据

提供一个 pattern 文件来进行匹配

grep -f pattern_file source_filename

不区分大小写

ld@ubuntu:/mnt/hgfs/haShare/test$ echo hello world | grep -i 'Hello'
hello world
ld@ubuntu:/mnt/hgfs/haShare/test$

包括或者排除,这个还有点问题,没弄明白,会继续更新。

--include
--exclude
出了问题,这个还需要继续弄清楚,,,未完待续。。。。。为什么只解析第一个文件
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -r --include *.{py,txt}
./flaskr.py:    """Closes the database again at the end of the request."""
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -r --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -R --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -R --include '*.{txt,py}'
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" . -R --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of"  -r --include *.{txt,py}
ld@ubuntu:/mnt/hgfs/haShare/test$ grep "of" ./  -r --include *.{txt,py}
./file1.txt:content of file 1
ld@ubuntu:/mnt/hgfs/haShare/test$
2013-01-28 14:23:32 zangchaodotcnatgmail 阅读数 653

1.

tar jcvf linux.2.6.25.8.tar.bz2 linux.2.6.25.8/

2.

mount -t nfs -o tcp,nolock 10.13.1.26:/xxx/nfs_root /mnt.
 
3.
root@zangchao:/work/system/linux-2.6.22.6# grep -l 'HAS_IOMEM' *
s3c2410_defconfig
root@zangchao:/work/system/linux-2.6.22.6# grep -n 'HAS_IOMEM' *
s3c2410_defconfig:1421:CONFIG_HAS_IOMEM=y
 

 grep  "smdk2410"  *  -nR
 
 4.
just check:
light32@zangchao:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/arm-linux-gcc/gcc-3.4.5-glibc-2.3.6/bin

for tmp:
light32@zangchao:~$ export  $PATH=usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/arm-linux-gcc/4.3.2/bin

for longterm:
light32@zangchao:sudo vim /etc/environment
light32@zangchao:~$ source /etc/environment 

5.
make distclean
make smdk2410_config
make

6.
light32@zangchao:/opt/bootloader/zangchao/u-boot-2012.04.01/u-boot-2012.04.01/board/samsung$ cp smdk2410/ smdk2440
cp: omitting directory `smdk2410/'

ard/samsung/smdk2440$ ls
libsmdk2410.o    lowlevel_init.S  smdk2410.c
lowlevel_init.o  Makefile         smdk2410.o

light32@zangchao:/opt/bootloader/zangchao/u-boot-2012.04.01/u-boot-2012.04.01/board/samsung/smdk2440$ cd -
/opt/bootloader/zangchao/u-boot-2012.04.01/u-boot-2012.04.01/board/samsung
------------------------
or:
cp smdk2410 smdk2440 -rf

7.

How do I use software from a PPA?

To start installing and using software from a Personal Package Archive, you first need to tell Ubuntu where to find the PPA.

Important: The contents of Personal Package Archives are not checked or monitored. You install software from them at your own risk.

If you're using the most recent version of Ubuntu (or any version from Ubuntu 9.10 onwards), you can add a PPA to your system with a single line in your terminal.

Step 1: On the PPA's overview page, look for the heading that reads Adding this PPA to your system. Make a note of the PPA's location, which looks like:

ppa:gwibber-daily/ppa

Step 2: Open a terminal and enter:

sudo add-apt-repository ppa:user/ppa-name

Replace ppa:user/ppa-name with the PPA's location that you noted above.

Screen shot of a terminal with the sudo add-apt-repoistory line

Your system will now fetch the PPA's key. This enables your Ubuntu system to verify that the packages in the PPA have not been interfered with since they were built.

Screen shot of a system retrieving the PPA details

Step 3: Now, as a one-off, you should tell your system to pull down the latest list of software from each archive it knows about, including the PPA you just added:

sudo apt-get update

Now you're ready to start installing software from the PPA!

8. ln -s /opt/
light32@zangchao:~/Desktop$ ln -s /opt/
light32@zangchao:~/Desktop$ ls
audacious.desktop     flashget.desktop       indicator-multiload.desktop  smplayer.desktop                zlnk-eztv.desktop
audacity.desktop      gcalctool.desktop      jedit.desktop                synaptic.desktop                zlnk-facebook.desktop
avidemux-gtk.desktop  geany.desktop          meld.desktop                 system-config-samba.desktop     zlnk-ttmeiju.desktop
cairo-dock.desktop    gnome-mplayer.desktop  openshot.desktop             ubuntu-software-center.desktop  zlnk-twitter.desktop
Chromium.desktop      google-chrome.desktop  opt                          uget-gtk.desktop                zlnk-youtube.desktop
diffuse.desktop       guake.desktop          psensor.desktop              virtualbox.desktop
eclipse.desktop       guayadeque.desktop     qq2012-test.desktop          vlc.desktop
emacs23.desktop       gvim.desktop           radiotray.desktop            wireshark.desktop
filezilla.desktop     htop.desktop           skype.desktop                xchat.desktop
light32@zangchao:~/Desktop$ 

9.
light32@zangchao:/opt/ubuntu12.04lts-x86$ dpkg -l | grep "virtual"
ii  gvfs                                      1.12.1-0ubuntu1.1                       userspace virtual filesystem - GIO module
ii  gvfs-backends                             1.12.1-0ubuntu1.1                       userspace virtual filesystem - backends
ii  gvfs-bin                                  1.12.1-0ubuntu1.1                       userspace virtual filesystem - binaries
ii  gvfs-common                               1.12.1-0ubuntu1.1                       userspace virtual filesystem - common data files
ii  gvfs-daemons                              1.12.1-0ubuntu1.1                       userspace virtual filesystem - servers
ii  gvfs-fuse                                 1.12.1-0ubuntu1.1                       userspace virtual filesystem - fuse server
ii  gvfs-libs                                 1.12.1-0ubuntu1.1                       userspace virtual filesystem - private libraries
ri  virtualbox-4.2                            4.2.12-84980~Ubuntu~precise             Oracle VM VirtualBox
light32@zangchao:/opt/ubuntu12.04lts-x86$ 


light32@zangchao:/opt/ubuntu12.04lts-x86$ sudo apt-get remove virtualbox-4.2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  kde-l10n-de language-pack-kde-de language-pack-kde-en language-pack-de-base
  language-pack-kde-zh-hans language-pack-kde-en-base kde-l10n-engb
  language-pack-kde-de-base kde-l10n-zhcn firefox-locale-de language-pack-de
  language-pack-kde-zh-hans-base amarok-help-de amarok-help-en
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  virtualbox-4.2
0 upgraded, 0 newly installed, 1 to remove and 140 not upgraded.
After this operation, 140 MB disk space will be freed.
Do you want to continue [Y/n]? 

light32@zangchao:~$ ps -aux | grep virtualbox
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
root      6483  0.0  0.0  20772  4904 ?        S    13:31   0:00 /usr/lib/virtualbox/VBoxXPCOMIPCD
root      6489 99.8  0.1  36088  8668 ?        Sl   13:31 170:02 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
root     14466  0.0  0.0   7436  1840 pts/2    S+   16:19   0:00 sudo apt-get remove virtualbox-4.2
root     14467  0.7  0.3  35972 25984 pts/2    S+   16:19   0:00 apt-get remove virtualbox-4.2
root     14483  0.4  0.5  46528 41392 pts/3    Ss+  16:20   0:00 /usr/bin/dpkg --status-fd 48 --force-depends --force-remove-essential --remove virtualbox-4.2:i386
root     14484  0.2  0.1  15092  9248 pts/3    S+   16:20   0:00 /usr/bin/perl -w /usr/share/debconf/frontend /var/lib/dpkg/info/virtualbox-4.2.prerm remove
root     14494  0.0  0.0   2236   600 pts/3    S+   16:20   0:00 /bin/sh /var/lib/dpkg/info/virtualbox-4.2.prerm remove
root     14538  0.0  0.0   7396  1512 pts/3    S+   16:20   0:00 whiptail --backtitle Package configuration --title Configuring virtualbox-4.2 --output-fd 11 --msgbox Running VMs found??VirtualBox is currently running. Please close it and try again. Please ?note that it can take up to ten seconds for VirtualBox (in particular ?the VBoxSVC daemon) to finish running. 11 76
light32  14637  0.0  0.0   5820   864 pts/4    S+   16:21   0:00 grep --color=auto virtualbox
light32@zangchao:~$ 


sudo dpkg -i virtualbox-2.2_2.2.0-45846_Ubuntu_intrepid_i386.deb*




 
 
-------------------------------------------------------------------

2009-09-18 21:47:00 s007sage 阅读数 1510

Linux下批量替换多个文件中的字符串的简单方法。用sed命令可以批量替换多个文件中的字符串。

sed命令可以批量替换多个文件中的字符串。
sed -i "s/
原字符串/新字符串/g" `grep 原字符串 -rl 所在目录`(千万注意这个符号,是最左上角那个符号不是单引号)

例如:我要把/test下所有包含abc的文件中的abc替换为def,执行命令:
sed -i "s/abc/def/g" `grep abc -rl /test`

这是目前linux最简单的批量替换字符串命令了!

具体格式如下:
sed -i "s/oldString/newString/g" `grep oldString -rl /path`

实例代码:sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl /usr/aa`
sed -i "s/
大小多少/日月水火/g" `grep 大小多少 -rl ./`

方法2

在日程的开发过程中,可能大家会遇到将某个变量名修改为另一个变量名的情况,如果这个变量是一个局部变量的话,vi足以胜任,但是如果是某个全局变量的话,并且在很多文件中进行了使用,这个时候使用vi就是一个不明智的选择。这里给出一个简单的shell命令,可以一次性将所有文件中的指定字符串进行修改:
grep "abc" * -R | awk -F: '{print $1}' | sort | uniq | xargs sed -i 's/abc/abcde/g'

替换字符:sed -i "s/old/new/g" /test

查找包含指定字符的文件名:grep aaa –ul  /test

2013-12-17 11:53:51 esrrhs 阅读数 479

把当前目录下内容是abc的替换成def


sed -i "s/abc/def/g" `grep "abc" -rl ./`

没有更多推荐了,返回首页