Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How do I use the grep command to process text?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Grep is a powerful text search tool that searches text using specific pattern matches (including regular expressions) and outputs matching lines by default. The Unix grep family includes grep, egrep, and fgrep.

Format usage:grep[options]…mode [file]…

Search for patterns in each file or standard input.

By default, a pattern is a basic regular expression (BRE).

Regular expression selection and interpretation:

-E pattern is an extended regular expression

-e pattern matching using patterns

-f Get pattern from file

-i Ignore case differences

-w mandatory pattern, matches only complete words

Look at the file. Take this file as an example.

root@zhaocheng ~]# cat filetestROOT:x:98:0:ROOT:/ROOT:/usr/localroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin

grep -E This is an extended regular expression that supports matching multiple elements and can be connected by pipe symbols.

[root@zhaocheng ~]# grep -E 'root|ROOT|sync' filetestROOT:x:98:0:ROOT:/ROOT:/usr/localroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncoperator:x:11:0:operator:/root:/sbin/nologin

grep -e This is a standard match, an e can only match one element, does not support pipes, if you want to match more than one, you need to add-e to match

[root@zhaocheng ~]# grep -e 'root' -e 'sync' filetestroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncoperator:x:11:0:operator:/root:/sbin/nologin

And there is also an egrep, which is equivalent to an extended version of grep, that is, grep -e does not support multi-element matching, does not support pipes, and egrep supports pipes.

egrep(grep -E in Linux) is an extended grep

[root@zhaocheng ~]# egrep 'root|sync' filetestroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncoperator:x:11:0:operator:/root:/sbin/nologin

grep -f This is generally used to check whether there are identical or duplicate lines in the two files. If there are, the duplicate lines will be output. This/etc/passwd is also a file. The previous filetest is also a file. Due to comparison, I deleted part of filetest. In this case, the effect will be obvious. In general, if there are identical lines in the two files, filter them out. You can use grep -f.

format grep -f file 1 file 2

[root@zhaocheng ~]# grep -f filetest /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin

grep -i matches regardless of case, this '' can also be left blank

[root@zhaocheng ~]# grep -i 'root' filetestROOT:x:98:0:ROOT:/ROOT:/usr/localroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

If you ignore case and want to match more, you can use egrep, which is equivalent to grep -E. This supports pipes and matches multiple lines.

[root@zhaocheng ~]# egrep -i 'root|halt' filetestROOT:x:98:0:ROOT:/ROOT:/usr/localroot:x:0:0:root:/root:/bin/bashhalt:x:7:0:halt:/sbin:/sbin/haltoperator:x:11:0:operator:/root:/sbin/nologin

The previous matches are all matching rows, and if you want to match only one word, you can use-w,

[root@zhaocheng ~]# egrep -w 'halt|sync' filetestsync:x:5:0:sync:/sbin:/bin/synchalt:x:7:0:halt:/sbin:/sbin/halt[root@zhaocheng ~]# egrep -w 'hat|syn' filetest

** Other parameters

-s No Message Suppress Error Message

-v anti-match selects mismatched rows

-V Display version information and exit

--help Display this help text and exit **

-s does not display error messages, such as without this file, add-s will not output error content

[root@zhaocheng ~]# grep kkkk /etc/shadowsgrep: /etc/shadows: No such file or directory[root@zhaocheng ~]# grep -s kkkk /etc/shadows

-v outputs all data except lp

[root@zhaocheng ~]# grep -v lp filetestlinuxaweqeeqwROOT:x:98:0:ROOT:/ROOT:/usr/localhalt$:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin#sync:x:5:0:sync:/sbin:/bin/syncmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinlinuxoooghhrhg

-V Output grep version information and exit

[root@zhaocheng ~]# grep -Vgrep (GNU grep) 2.20Copyright (C) 2014 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later .This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Written by Mike Haertel and others, see .

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report