


Detailed explanation of how to use grep command to retrieve file contents in Linux
这篇文章主要给大家介绍了关于Linux中利用grep命令如何检索文件内容的相关资料,并且给大家简单介绍了关于Linux grep和find的区别,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
前言
Linux系统中搜索、查找文件中的内容,一般最常用的是grep命令,另外还有egrep命令,同时vi命令也支持文件内容检索。下面来一起看看Linux利用grep命令检索文件内容的详细介绍。
方法如下:
1、搜索某个文件里面是否包含字符串
命令格式:grep "被查找的字符串" filename1
例如:
grep "0101034175" /data/transaction.20170118.log
2、在多个文件中检索某个字符串
命令格式:
grep "被查找的字符串t" filename1 filename2 filename3 ...
grep "被查找的字符串" *.log
3、显示所检索内容在文件中的行数,可以使用参数-n
命令格式: grep -n "被查找的字符串" *.log
4、检索时需要忽略大小写问题,可以使用参数“-i”
命令格式: grep -i "被查找的字符串" *.log
5、从文件内容查找不匹配指定字符串的行
命令格式:grep –v "被查找的字符串" 文件名
6、搜索、查找匹配的行数:
命令格式:
grep –c "被查找的字符串" 文件名
grep "被查找的字符串" 文件名 | wc -l
7、递归搜索某个目录以及子目录下的所有文件
命令格式:grep –r "被查找的字符串" 文件目录
8、获取哪些文件包含搜索的内容,并列出文件名
命令格式:grep -H –r "被查找的字符串" 文件目录 | cut -d: -f1 [| uniq]
例如:
grep -H -r "v\$temp_space_header" /u01/app/Oracle/product/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1 grep -H -r "v\$temp_space_header" /u01/app/oracle/product/11.1.0/dbhome_1/rdbms/admin/ | cut -d: -f1 | uniq
9、获取与整个搜索字符匹配的内容
命令格式:grep –w "被查找的字符串" 文件名
10、grep命令与find 命令的结合,实现联合检索
命令格式:find . -name '*.sql' -exec grep -i '
被检索内容 ' {} \; -print
例如:
find . -name '*.sql' -exec grep -i 'v\$temp_space_header' {} \; -print
Linux grep和find的区别
这是两个不同的命令,关于grep:
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。
而linux下的find(具体使用方法可以参考这里:http://www.jb51.net/article/108198.htm)
功能:在目录结构中搜索文件,并执行指定的操作。此命令提供了相当多的查找条件,功能很强大。
语法:find 起始目录 寻找条件 操作
说明:find命令从指定的起始目录开始,递归地搜索其各个子目录,查找满足寻找条件的文件并对之采取相关的操作。
所以简单点说说,grep是查找匹配条件的行,find是搜索匹配条件的文件
The above is the detailed content of Detailed explanation of how to use grep command to retrieve file contents in Linux. For more information, please follow other related articles on the PHP Chinese website!

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor