|

Grep 2 Lines using `grep` Command in Linux

grep is excellent to match patterns from STDOUT/text files in command line or scripts. It’s handy. Sometimes, our problem is more complex than finding a keyword from a file. On a first thought, it may sound impossible using grep for such complex problems. But grep can be quite powerful than we thought. Today, let’s check one example.

The problem to grep

The problem is as follows. We want to check whether the ~/.bashrc file contains 2 consecutive lines (okay to assume they are not at the beginning of the file):

export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH

There are 2 criteria: 1. these 2 lines must both exist, and 2. these 2 lines are consecutive.

The difficulties with grep

Right, the basic pattern format by grep has difficulties to understand the \n (new line) as the input is matched line by line. But we don’t need to stop here. grep has many options. The key is to make it match \n. First, we need to make grep not use \n as the line separator. Second, we need to make grep treat \n as a normal character.

The solution

For the first purpose, the -z option is useful. It makes grep “treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.”. For the second purpose, the -P option is useful. It makes grep use Perl-compatible regular expression (PCRE) grammar which can treat \n as a normal character.

With all these analysis, we can build the command now:

grep -Pzl '\nexport GOPATH=\$HOME/go\nexport PATH=\$GOPATH/bin:\$PATH\n' \
~/.bashrc

The first \n make sure the first line start after a \n. The last \n makes sure the second line is in a single line instead of a head of a longer line. \$ is used because $ in PCRE is a special character.

The same technique can be used for perl one-liners too: How to Match Multiple Lines using Regex in Perl One-liners.

Similar Posts

  • MFC程序使用系统风格界面

    VC6默认编译出来的程序在XP下Luma风格下运行也是Windows的经典界面, 有损界面的美观与统一. VC2008默认设置下如果不是使用的unicode也是如此. 本文给出使VC6和VC2008可以编译出使用系统界面风格的解决方案. 1. 使VC6编译出使用系统风格的程序 步骤如下: 1) 创建一个.manifest文件的资源. 在res/文件夹下创建一个跟以程序名加.manifest的文件, 如果程序为test.exe, 则创建test.exe.manifest 文件可由此下载: https://www.systutorials.com/t/g/programming/resultcollector.manifest/ 注意要使用utf-8编码保存。 2) 将新定义的资源加入到.rc2文件中, 类型设为24. 打开res/文件夹下的.rc2文件, 在其中加入如下定义: 1 24 MOVEABLE PURE “res/test.exe.manifest” 其中的文件地址按1)步中修改的设置即可. 之后编译即可, 为了使程序界面可能充分利用系统的界面特性, 可以将界面字体设置为TrueType类型的, 利用Windows XP等系统的屏幕字体平滑特性. 2. 使VC2008编译出使用系统风格的程序 在VC2008下就比较简单了, 如果程序字符集使用unicode则默认就是使用系统界面风格的, 如果选择其它的类型, 则编辑下stdafx.h即可. 最后面部分找到这么一段: #ifdef _UNICODE #if defined _M_IX86 #pragma comment(linker,”/manifestdependency:”type=’win32′ name=’Microsoft.Windows.Common-Controls’ version=’6.0.0.0′ processorArchitecture=’x86′ publicKeyToken=’6595b64144ccf1df’ language=’*'””) #elif defined _M_IA64 #pragma comment(linker,”/manifestdependency:”type=’win32’…

  • x86-64 ISA / Assembly Programming References

    This post collect the reference resource for x86-64 (also know as Intel 64, AMD 64) ISA / assembly language programming. x86-64 is a 64-bit extension of the Intel x86 instruction set. ==x86-64 Assembly Programming== Introduction to Computer Systems Resources (15-213 Introduction to Computer Systems Resources from CMU) Lots materials for learning machine-level programming on the…

  • How to make WordPress regenerate the thumbnails after I changing the media aspects?

    I recently changes the media aspects. The problem is that the existing thumbnails are not changed to the new aspects. How to make WordPress regenerate the thumbnails after I changing the media aspects? The “Regenerate Thumbnails” works like a charm for me: https://wordpress.org/plugins/regenerate-thumbnails/ . It allows you to regenerate your thumbnails after changing the thumbnail…

Leave a Reply

Your email address will not be published. Required fields are marked *