Avatar of lolaferrari
lolaferrariFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

help with grep in script

I have a text file below where I only want to extract the line NAME and then the first two occurences of "file was .." afterwards but i can't get beyond what i've tried here - any ideas?

cat test | egrep  '(^NAME |file)'

 textfile

NAME test
FILE: 2.0
SUBVERSION 1.0
HEADER: 1.3

        file was ( here )
        file was ( there )
        file was ( everywhere )
        file was ( everywhere )
        file was ( everywhere )
        file was ( here )
        file was ( there )
        file was ( everywhere )
        file was ( everywhere )
        file was ( everywhere )

junk is
here to stay
and on
and on

NAME test2
FILE 1.0
SUBVERSION 1.0
HEADER: 1.4

        file was ( here )
        file was ( there )
        file was ( everywhere )
        file was ( everywhere )
        file was ( everywhere )

junk is
here to stay
and on
and on

NAME test67
FILE 3.0
SUBVERSION 1.0
HEADER: 1.4

        file was ( here )
        file was ( there )
        file was ( everywhere )
        file was ( everywhere )
        file was ( everywhere )
Unix OS

Avatar of undefined
Last Comment
Gerwin Jansen
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
I'm thinking of an awk or sed script don't think you can do this with just grep. Can you use awk / sed here?
Avatar of lolaferrari
lolaferrari
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Perfect and thank you for the very fast response
@farzanj - nice :) - I just get an error on the last grep and have to change to:

egrep '^NAME|file was'  test | grep -A2 'NAME' | grep -v '^-'
Unix OS
Unix OS

Unix is a multitasking, multi-user computer operating system originally developed in 1969 at Bell Labs. Today, it is a modern OS with many commercial flavors and licensees, including FreeBSD, Hewlett-Packard’s UX, IBM AIX and Apple Mac OS-X. Apart from its command-line interface, most UNIX variations support the standardized X Window System for GUIs, with the exception of the Mac OS, which uses a proprietary system.

33K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo