Search code examples
linuxbashawksedcut

How use cut/awk/sed command to select as records from a file in linux


I have a file (details.txt) with content in below format.

Serial Number: 0xf
Name: XXX
Age: 25
Sex: Male
Serial Number: 0xe
Name: YYY
Age: 27
Sex: FeMale

I want to retrieve complete record of a person with Name say "XXX".

How can it be achieved with cut command?


Solution

  • You can simply use grep like this:

    grep -B1 -A2 "Name: XXX"
    

    -B1 and -A2 mean that grep should also print one line before and two lines after the match.