Common linux commands (1) grep

Source: Internet
Author: User

Common linux commands (1) grep

Grep is used to find strings.

Case 1: Get the user's home directory

For example, if the user name is whuang, How can I obtain his home directory?

grep "^whuang:"  /etc/passwd |awk -F : {'print $6'}

Case 2: Get the user id
grep "^whuang:" /etc/passwd |awk -F : {'print $3'}

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxjb2RlIGNsYXNzPQ = "hljs perl"> Case 3: Get the group id

grep "^whuang:" /etc/passwd |awk -F : {'print $4'}
Case 4: Get the group name of the specified user
grep "^whuang:" /etc/passwd |awk -F : {'print $4'}|xargs -i grep {} /etc/group|awk -F : {'print $1'}whuang
Case 5: Process for obtaining the specified port
netstat -anp |grep "^[a-z]\+[ ]\+0[ ]\+0[ ]\+[0-9\.]\+:80[ ]\+"|awk -F" " {'print $7'}|cut -d"/" -f1
Case 6
Grep 'like' test. c (in the file test. search for rows containing "like" in c) grep-n 'like' test. c (output row number) [root @ localhost awk-study] # grep-n 'like' test. c3: I like java hsf-txt

Note: When you use + to indicate multiple characters, you must add the escape character before +.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.