Grep a part of file based on string identifiers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a part of file based on string identifiers
# 1  
Old 03-20-2013
Grep a part of file based on string identifiers

consider below file contents

Code:
cat myOutputFIle.txt
8 CCM-HQE-ResourceHealthCheck:
Resource List :
No RED/UNKNOWN resource Health entries found
----------------------------------------------------------
9 CCM-TraderLogin-Status:
Number of logins: 0
----------------------------------------------------------
10 CCM HA Status:
Server:XXX.XX.XX.XX not enabled
Server Role:ACTIVE
Server State:AVAILABLE
Network State:GY_PY
----------------------------------------------------------
11 MG-DigitalPortChannel-Status:
No MG's are present hence terminating script
----------------------------------------------------------
12 MM-Operational-State:
HTML Code:
ipAddress       requestOperationalState
XX.xx.XX.XX.XX   MS_INTRAFFIC
----------------------------------------------------------


what i want to achieve is grep a string let say "CCM HA Status:" and display all the lines below it till it reaches "-----------------------------------------"

i implemented partially using sed command. .but its displaying output allong with identifiers..

Code:
[root@htmlgen]# sed -n "/CCM HA Status:/,/----------------------------------------------------------/ w tablextract2.sql" <myOutputFIle.txt
[root@ htmlgen]#
[root@duhtmlgen]# cat tablextract2.sql
10 CCM HA Status:
Server:xxx.xx.xxx.xxHA not enabled
Server Role:ACTIVE
Server State:AVAILABLE
Network State:GY_PY
----------------------------------------------------------
[root@ htmlgen]#


what the expected output is

Code:
Server:xxx.xx.xxx.xxHA not enabled
Server Role:ACTIVE
Server State:AVAILABLE
Network State:GY_PY

similarly if i search for "MM-Operational-State:" it should display below output

Code:
ipAddress       requestOperationalState
XX.xx.XX.XX.XX   MS_INTRAFFIC

any help is appreciated.. thanks

Last edited by vivek d r; 03-21-2013 at 01:27 AM..
# 2  
Old 03-20-2013
Code:
sed -n '/CCM HA Status/,/----------/p' file > tablextract.sql

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-20-2013
Code:
awk '/CCM HA Status/ {f=1; next} /^-----/ {f=x} f' infile

If the other pattern is needed, just alter the pattern or add it with a "or" ||, just as you need it.
This User Gave Thanks to zaxxon For This Post:
# 4  
Old 03-20-2013
Code:
$ sed -n "/CCM HA Status:/,/---------/{/CCM HA Status:/d;/---------/d;p;}" file
Server:XXX.XX.XX.XX not enabled
Server Role:ACTIVE
Server State:AVAILABLE
Network State:GY_PY

Or

Code:
$ awk ' /CCM HA Status:/,/---------/ { if( !(/CCM HA Status:/ || /---------/) ) print } ' file
Server:XXX.XX.XX.XX not enabled
Server Role:ACTIVE
Server State:AVAILABLE
Network State:GY_PY

This User Gave Thanks to anbu23 For This Post:
# 5  
Old 03-20-2013
thanks everyone... :-) works good
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Discard part of a file based on a pattern ---

I have the file: s3_T0(2) Pos "1" "2" s1_T1(2) Pos "1" "2" --- 0 0 1 0 0 1 1 1 --- 1 2 "tau0" 1 2 "h10" I want to patternmatch on --- and get only the third part i.e. 1 2 "tau0" 1 2 "h10" I wanted to start simple but even something like (5 Replies)
Discussion started by: eagle_fly
5 Replies

2. Shell Programming and Scripting

How to grep the required part from the string?

Hi All, I am trying to fetch the particular content from the result of grep command. I am using ps-ef |grep engine| awk '{print $6}' above statement giving me /opt/test/user/domain/CORPTEST/application/CacheScheduler/CacheScheduler-CacheScheduler but I want (13 Replies)
Discussion started by: sharsour
13 Replies

3. Shell Programming and Scripting

Match part of string in file2 based on column in file1

I have a file containing texts and indexes. I need the text between (and including ) INDEX and number "1" alone in line. I have managed this: awk '/INDEX/,/1$/{if (!/1$/)print}' file1.txt It works for all indexes. And then I have second file with years and indexes per year, one per line... (3 Replies)
Discussion started by: phoebus
3 Replies

4. Shell Programming and Scripting

Remove part of a file based on identifiers

here below is a part of the file cat fileName.txt NAME=APP-VA-va_mediaservices-113009-VA_MS_MEDIA_SERVER_NOT_PRESENT-S FIXED=false DATE= 2013-02-19 03:46:04.4 PRIORITY=HIGH RESOURCE NAME=ccm113 NAME=APP-DS-ds_ha-140020-databaseReplicationFailure-S FIXED=false DATE= 2013-02-19... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

cut a line into different fields based on identifiers

cat fileanme.txt custom1=, custom2=, userPulseId=3005, accountPolicyId=1, custom3=, custom4=, homeLocationId=0, i need to make the fields appear in next line based on identifier (,) ie comma so output should read cat fileanme.txt custom1=, custom2=, userPulseId=3005, ... (8 Replies)
Discussion started by: vivek d r
8 Replies

6. Shell Programming and Scripting

Possible to grep string based on surrounding strings?

I was wondering if it was possible to grep a pattern based on the surround text. For example, if i have an input file like this: titleA titleB titlex titleC titleD titlex titleE And I want to grep "title" and save the results only if it is not followed with a "titlex". My output... (14 Replies)
Discussion started by: jl487
14 Replies

7. Shell Programming and Scripting

Extracting lines based on identifiers into multiple files respectively

consider the following is the contents of the file cat 11.sql drop procedure if exists hoop1 ; Delimiter $$ CREATE PROCEDURE hoop1(id int) BEGIN END $$ Delimiter ; . . . . drop procedure if exists hoop2; Delimiter $$ CREATE PROCEDURE hoop2(id int) BEGIN END $$ (8 Replies)
Discussion started by: vivek d r
8 Replies

8. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

9. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

10. Shell Programming and Scripting

sort entire line based on part of the string

hey gurus, my-build1-abc my-build10-abc my-build2-abc my-build22-abc my-build3-abc basically i want to numerically sort the entire lines based on the build number. I dont zero pad the numbers because thats "how it is" ;-) sort -n won't work because it starts from the beginning. ... (10 Replies)
Discussion started by: gurpal2000
10 Replies
Login or Register to Ask a Question