Wednesday, January 30, 2013

GREP command : How to use ??


GREP:
Three version of grep command in UNIX 'grep, fgrep, egrep'.
'fgrep' stands for Fixed 'grep'.
'egrep' Extended 'grep'.

1. To find relevant word and exclusion irrelevant word.
# grep Exception logfile.txt | grep -v ERROR

2.If you want to count of a particular word in log file you can use grep -c option to count the word.
# grep -c "Error" logfile.txt

3. grep --context option allows us to print lines around matching pattern. Below example of grep command in UNIX will print 6 lines around matching line of word "successful" in logfile.txt
# grep --context=6 successful logfile.txt
# grep -C 2 'hello' *
Prints two lines of context around each matching line.

4. egrep stands for extended grep and it is more powerful than grep command in Unix and allows more regular exception
# egrep 'Error|Exception' logfile.txt

5. If you want to do case insensitive search than use -i option from grep command in UNIX. Grep -i will find occurrence of both Error, error and ERROR
# grep -i Error logfile

6. Below command will print all files which have "Error" on them. ZGREP used to perform same operation as grep does but with .gz files
# zgrep -i Error *.gz

7. Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'.
# grep -w ERROR logfile
# grep 'ERROR>' *
Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.

8. "grep -l" which display only the file names which matches the given pattern. Below command will only display file names which have ERROR
# grep -l ERROR *.log

9. If you want to see line number of matching lines you can use option "grep -n" below command will show on which lines Error has appeared.
# grep -n ERROR log file.

10. If you want to do recursive search using grep command in Unix there are two options either use "-R" command line option or increase directory one by one as shown below.
Read more ...

grep命令基础练习题


常用的grep选项是:

-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符)。
-h 查询多文件时不显示文件名。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的所有行。

文件格式:

第1列:城市位置编号。
第2列:月份。
第3列:存储代码及出库年份。
第4列:产品代号。
第5列:产品统一标价。
第6列:标识号。
第7列:合格数量。

file.txt文件内容:
48      Dec     3BC1977 LPSX   68.00   LVX2A   138
483     Sept    5AP1996 USP    65.00   LVX2C   189
47      Oct     3ZL1998 LPSX   43.00   KVM9D   512
219     dec     2CC1999 CAD    23.00   PLV2C   68
484     nov     7PL1996 CAD    49.00   PLV2C   234
483     may     5PA1998 USP    37.00   KVM9D   644
216     sept    3ZL1998 USP    86.00   KVM9E   234

练习:

1. 含有“48”字符串的行的总数
[root@www.linuxidc.com]# grep -c 48 file.txt
4

2. 显示含有“48”字符串的所有行的行号
[root@www.linuxidc.com]# grep -n 48 file.txt
1:48      Dec     3BC1977 LPSX    68.00   LVX2A   138
2:483     Sept    5AP1996 USP     65.00   LVX2C   189
5:484     nov     7PL1996 CAD     49.00   PLV2C   234
6:483     may     5PA1998 USP     37.00   KVM9D   644

3. 精确匹配只含有“48”字符串的行
[root@www.linuxidc.com]# grep "48\>" file.txt
48      Dec     3BC1977 LPSX    68.00   LVX2A   138

PS:使用grep抽取精确匹配的一种更有效方式是在抽取字符串后加\ >

4. 抽取代码为4 8 4和4 8 3的城市位置
[root@www.linuxidc.com]# grep -E "48[3|4]\>" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644

PS:使用[ ]来指定字符串范围

5. 显示使行首不是4或8
[root@www.linuxidc.com]# grep -v "^[4|8]" file.txt

[root@www.linuxidc.com]# grep -v "^[48]" file.txt

[root@www.linuxidc.com]# grep -v "^[4,8]" file.txt

[root@www.linuxidc.com]# grep "^[^48]" file.txt       #这个是直接最简单的方法
219     dec     2CC1999 CAD     23.00   PLV2C   68
216     sept    3ZL1998 USP     86.00   KVM9E   234

PS:可以在方括号中使用^记号,表示行首不是某字符

6. 显示含有九月份的行
[root@www.linuxidc.com]# grep -i Sept file.txt

[root@www.linuxidc.com]# grep -E "[Ss]ept" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
216     sept    3ZL1998 USP     86.00   KVM9E   234

PS :使用- i开关可以屏蔽月份S e p t的大小写敏感

7. 显示以K开头,以D结尾的所有代码
[root@www.linuxidc.com]# grep "K...D" file.txt
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
483     may     5PA1998 USP     37.00   KVM9D   644

PS:点.代表任意一个字符,由于代码是五位字符的字符串,所以中间用三个点表示任意字符

8. 显示头两个是大写字母,中间两个任意,并以C结尾的代码
[root@www.linuxidc.com]# grep "[A-Z][A-Z]..C" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
219     dec     2CC1999 CAD     23.00   PLV2C   68
484     nov     7PL1996 CAD     49.00   PLV2C   234

PS:[A-Z]表示任意一个大写字母

9. 查询所有以5开始以1 9 9 6或1 9 9 8结尾的所有记录
[root@www.linuxidc.com]# grep "5..199[68]" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
483     may     5PA1998 USP     37.00   KVM9D   644

10. 假定要取得城市代码,第一个字符为0-9,第二个字符在0到5之间,第三个字符在0到6之间
[root@www.linuxidc.com]# grep "[0-9][0-5][0-6]" file.txt        #一般情况下很多人会想到使用这个错误方式,没考虑到最后一列也是三位数字
47      Oct     3ZL1998 LPSX    43.00   KVM9D   512
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644
216     sept    3ZL1998 USP     86.00   KVM9E   234

[root@www.linuxidc.com]# grep "^[0-9][0-5][0-6]" file.txt
216     sept    3ZL1998 USP     86.00   KVM9E   234

11. 抽取包含数字4至少重复出现两次的所有行
[root@www.linuxidc.com]# grep "4\{2,\}" file.txt
483     may     5PA1998 USP     37.00   KVM9D   644

12. 匹配8重复出现2到6次,并以3结尾
[root@www.linuxidc.com]# cat num.txt
83.
888883
8884
88883

[root@www.linuxidc.com]# grep "8\{2,6\}3" num.txt
888883
88883

13. 匹配特殊字符
查询有特殊含义的字符,诸如$ . ' " * [] ^ |\ + ? ,必须在特定字符前加\进行转义
如要查询文件名c o n f t r o l l . co n f(这是一个配置文件)
[root@www.linuxidc.com]# grep "conftroll\.conf" file.txt

14. 查询格式化文件名
格式:最多六个小写字符,后跟句点,接着是两个大写字符
[root@www.linuxidc.com]# grep "[a-z]\{1,6\}\.[A-Z]\{2\}" file.txt

15. 查询IP地址
要查看n n n . n n n网络地址
[root@www.linuxidc.com]# grep "[0-9]\{3\}\.[0-9]\{3\}\." 的IP地址范围为1.0.0.1-127.255.255.254;

16. 类名
[ [ : u p p e r : ] ][ A - Z ] [ [ : a l n u m : ] ] [ 0 - 9 a - zA-Z]
[ [ : l o w e r : ] ] [ a - z ] [ [ : s p a c e : ] ] 空格或t a b键
[ [ : d i g i t : ] ] [ 0 - 9 ] [ [ : a l p h a : ] ] [ a - z A - Z ]
围为:192.0.1.1-223.255.255.254

取以5开头,后跟至少两个大写字母
[root@www.linuxidc.com]# grep "5[[:upper:]]\{2,\}" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
483     may     5PA1998 USP     37.00   KVM9D   644

取以P或D结尾的所有产品代码
[root@www.linuxidc.com]# grep "[[:upper:]]\{2\}[P,D]" file.txt
483     Sept    5AP1996 USP     65.00   LVX2C   189
219     dec     2CC1999 CAD     23.00   PLV2C   68
484     nov     7PL1996 CAD     49.00   PLV2C   234
483     may     5PA1998 USP     37.00   KVM9D   644
216     sept    3ZL1998 USP     86.00   KVM9E   234
Read more ...

Wednesday, December 12, 2012

Mac OS X 下用 iTunes 截取音乐任意部分的方法

右击进入歌曲 显示简介->选项,里面有“起始时间”和“停止时间”两个选择,填入你刚才记录的时间段。

选中歌曲,右击选择“转换所选内容为ACC”。就可以完成音乐截取了。
Read more ...

Tuesday, December 11, 2012

Mac OS X 下直接从视频中提取音频文件

  • 找到并右键点击需要进行操作的视频文件
  • 在弹出的右键菜单中选择“编码所选视频文件”(Encode Selected Video Files)
  • 在弹出的“编码媒体”(Encode Media)对话框的“Setting”下拉菜单中选择“Audio Only”
  • 选择好目标文件的路径后点击“Continue”即可
Read more ...

Problem with printing PDFs from Safari

Problem:
When I view a PDF in Safari it looks good. When I go to print it either the preview is totally blank or one page that I was viewing at the time previews and the print dialog box shows the additional 8 pages but they are all blank. I'm baffled. If I use Chrome and go from its interface to preview it works perfectly. Thank you for any guidance.

Solution:
Go to /Library/Internet Plug-Ins
Move the Adobe PDF plugin to the Trash.
Quit then relaunch Safari.
Read more ...

Thursday, December 6, 2012

iTunes 播放 ape flac

APE是Monkey's Audio,一种无损压缩格式。这种格式的压缩比远低于其他音频格式,但能够做到真正无损,同时其开放源码的特性,也获得了不少音乐发烧友的青睐。在现有不少无损压缩方案中,APE是一种有着突出性能的格式,令人满意的压缩比以及飞快的压缩速度,在国内应用比较广泛,成为了不少朋友私下交流发烧音乐的选择之一。

由于其无损压缩的特性,造就了APE音乐CD级的音质输出,同时接近原文件一半大小的压缩比,也减少了存储空间的占用,而通过MP3这种目前主流的播放器来表现,则更为人性化。
      具体表现在:
  1.高压缩比:APE的压缩比大约在55.50%,接近源文件的一半大小。

  2.编码速度快:编码速度考验用户的耐心,同FLAC(国内目前只有台电T19支持FLAC无损压缩音频格式)一样,APE有着超快的编码速度,这在目前众多的音频编码标准中具备了同FLAC相同的速度优势。

  3.平台的广泛支持:音频压缩不但需要硬件的支持,也需要的软件的支持,因此能够被更广泛的平台支持,也就意味着被更多用户使用。FLAC与APE在这方面做的都非常出色,能够兼容所有系统平台,现在无论您是Windows用户还是众多版本的Linux用户,哪怕您是Mac OS的忠实FANS,都无需担心无法使用FLAC或APE。

  4.开源特性:同FLAC一样,APE具备开源特性,这意味着任何组织或个人都可以免费使用这种压缩技术,任何组织或个人都可以修改和发布基于这两种技术的新产品,这给众多MP3厂商降低成本提供了有力保障,且消费者也能够以相对低廉的价格购买到只有世界级MP3(例如:iPod支持ALAC)才支持的无损压缩音频、CD级的音质表现!

  5.资源的广泛性:庞大的网路资源,决定了APE的普及度,用户只需通过网路搜索引擎,即可找到数量众多的APE下载资源,无论您使用BT还是讯雷皆可轻松获取APE资源。

       和APE一起的CUE文件有什么用?
       通常我们下载的APE文件有两种:一种是下载一张专辑中有好几个APE文件一个文件一首歌,就和我们平时听的MP3文件一样。还有一种是一张专辑只有单个APE文件,并且附有一个CUE文件。其实这个CUE文件很小,但是非常有用,它记录着这个大的APE文件中的音乐的段落,音乐的名称以及作者专辑等内容。对于第一种APE,您就可以像播放MP3一样,将所有的APE文件全加到千千静听中,进行播放就行了。对于第二种APE,如果只把APE文件加到播放器中,你就会发现一整张专辑成了一首歌。所以对于第二种APE,你要将APE带的CUE文件利用Foobar、千千静听打,这些播放软件就会自动依照CUE中的内容,将指定的APE文件分段后在播放列表中出现。如果你想把这个APE文件还原成CD的话,也要利用到这个CUE文件。

然后打开偏好设置,进行如下设置。
点 import settings


然后去网上寻找一款可以加载虚拟CD的软件比如DAEMTOOLS,先点击挂载,选择想要挂在的文件,选择以CUE后缀结尾的文件。接着会自动跳出虚拟CD,ITUNES 会自动跳出,询问你是否需要导入CD。导入后还可以自己添加专辑图片。选中全部音乐然后get info,在artwork中添加相应图片即可。

Read more ...

Sunday, November 11, 2012

彻底关闭win8的休眠和睡眠功能

右键cmd,以管理员权限运行

powercfg -h off 命令来关闭休眠.

关闭睡眠
输入 gpedit.msc (组策略)
计算机设置-管理模板-系统-电源管理
电源管理-睡眠设置
将“睡眠时允许待机(S1-S3)(接通电源)”设置为“已禁用”
将“睡眠时允许待机(S1-S3)(使用电池)”设置为“已禁用”


重启生效!
Read more ...

Thursday, November 8, 2012

MAC下德语助手无限使用

找到文件
/Users/xxx/Library/Preferences/com.eusoft.dehelper.plist
xxx为用户名

用xcode打开,也可以用PlistEdi Pro 打开。
找到 MAIN_TimesLeft
这个就是使用次数,把它修改位10000000就行了。

这样就可以长期使用了。不过如果你方便买正版的话还是花点钱吧。
Read more ...

Tuesday, October 16, 2012

Print plot in MATLAB in Mac OS X

If you receive an error when using the File-> Print option in Mac OS X in MATLAB.

Problem sending file to output device, system returned error :
lpr: The printer or class was not found.

...

Just follow the steps.

1. In Safari, navigate to http://localhost:631
2. Click on the “Administration” tab, and then “Edit Configuration File”
3. At the top of the editable block of text, add these lines:

Printcap /etc/printcap
PrintcapFormat BSD # For Snow Leopard only


4. Click “Save Changes”. It may take 30 seconds or longer for these changes to go into effect.
5. Restart MATLAB.
Read more ...

Monday, October 8, 2012

Run MATLAB script in terminal

Just type command like following.

matlab -nojvm -nodesktop -r "run <the-script>.m"
matlab -nojvm -nodesktop -r "<the-script>"
matlab -nojvm -nodesktop -r "run <the/path>/<the-script>.m"
Read more ...

MATLAB runtime_error solution

When you running MATLAB, if you encountered an error like this:

terminate called after throwing an instance of 'std::runtime_error'  what():  locale::facet::_S_create_c_locale name not valid

It is easy to solve just by setting environmental variable LC_ALL="en_US.utf8"

bash: export LC_ALL="en_US.utf8"
csh: setenv LC_ALL "en_US.utf8"

It is better to add this to your bash or csh profile.
e.g.
.bshrc
.cshrc
.bash_profile
Read more ...

Thursday, October 4, 2012

如何删除Mac App Store中别人帐号已购应用程序的更新提示

如果安装了别人破解的应用程序,Mac App Store 提示应用程序有更新,会需要输入别人的AppleID信息。如果要清除提示。

1. 在Mac App Store中安装一个免费的App,然后在“应用程序”中找到这个App,右击 –> “显示包内容”–> 进入文件夹“Contents”–> 拷贝“_CodeSignature”,“_MASReceipt”。

2. 在“应用程序”中找到那个破解的App,右击 –> “显示包内容”–> 进入文件夹“Contents”,把刚才拷贝的“_CodeSignature”,“_MASReceipt”粘贴进来覆盖原来的。
Read more ...