Need to extract the word after a particular keyword throughout the file..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to extract the word after a particular keyword throughout the file..
# 1  
Old 01-05-2016
Need to extract the word after a particular keyword throughout the file..

Hi Everyone,

Need help in extracting the hostname from the below output.

Expected output:
Code:
DS-TESTB-GDS-1.TEST.ABC.COM
DS-TESTB-GDS-2.TEST.ABC.COM
....
...

Code:
[localhost.STAGE.XYZ.COM] /tmp $ cat -n /tmp/patchreport
     1  /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches --box=DS-TESTB-GDS-1.TEST.ABC.COM --box=DS-TESTB-GDS-2.TEST.ABC.COM --box=DS6-TESTA-APP-1.TEST.ABC.COM --box=DS6-TESTA-ASYNC-1.TEST.ABC.COM --box=DS6-TESTA-ER-1.TEST.ABC.COM --box=DS6-TESTA-MAA-1.TEST.ABC.COM --box=DS6-TESTA-MAPP-1.TEST.ABC.COM --box=DS6-TESTA-MAW-1.TEST.ABC.COM --box=DS6-TESTA-SCAN-1.TEST.ABC.COM --box=DS6-TESTA-SEARCH-1.TEST.ABC.COM --box=DS6-TESTA-SERVICE-1.TEST.ABC.COM --box=DS6-TESTA-TRACKER-1.TEST.ABC.COM --box=DS6-TESTA-WWW-1.TEST.ABC.COM --box=FIN-DEV-UC4DB-1.TEST.ABC.COM --box=MDS-DEV-APP-2.TEST.ABC.COM --box=MDS-DEV-DB-2.TEST.ABC.COM --box=MON-TEST-LOG-3.TEST.ABC.COM --box=MON-TEST-LOGIDX-3.TEST.ABC.COM --box=MOS-DEV-DB-1.TEST.ABC.COM
     2  /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches --box=MCS-DEV-ARDAPP-1.TEST.ABC.COM --box=MCS-DEV-DB-1.TEST.ABC.COM --box=MCS-DEV-IDDIR-1.TEST.ABC.COM --box=MCS-DEV-PASSCENT-1.TEST.ABC.COM --box=MCS-DEV-PASSDB-1.TEST.ABC.COM --box=MCS-DEV-PASSPORT-1.TEST.ABC.COM --box=MCS-DEV-PASSPORT-2.TEST.ABC.COM --box=MCS-DEV-PASSPORT-3.TEST.ABC.COM --box=MCS-GRVDEV-DPOPS-1.TEST.ABC.COM --box=MON-DEV-NMS-1.TEST.ABC.COM --box=XORN.STAGE.XYZ.COM
     3  /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches --box=JMS-DEV-FILE-1.STAGE.XYZ.COM --box=ROGUE.STAGE.XYZ.COM
-

Thiyags.

Last edited by Don Cragun; 01-05-2016 at 06:23 PM.. Reason: Add CODE tags.
# 2  
Old 01-05-2016
Please use code tags as required by forum rules!

How about
Code:
grep -o "\-\-box=[^ ]*" file | cut -f2 -d=
DS-TESTB-GDS-1.TEST.ABC.COM
DS-TESTB-GDS-2.TEST.ABC.COM
DS6-TESTA-APP-1.TEST.ABC.COM
.
.
.


Last edited by RudiC; 01-05-2016 at 06:16 AM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-05-2016
Hi RudiC,

Thanks for the reply, It works good as i expected...
-
Thiyags.
# 4  
Old 01-05-2016
Hello Thiyagoo,

Welcome to forums, please use code tags as per forum rules for commands/codes/Inputs you use in your posts.
Following may help you too in your query.
Code:
awk '{for(i=1;i<=NF;i++){match($i,/=.*COM/);if(substr($i,RSTART+1,RLENGTH-1)){print substr($i,RSTART+1,RLENGTH-1)}}}' Input_file

Output will be as follows.
Code:
DS-TESTB-GDS-1.TEST.ABC.COM
DS-TESTB-GDS-2.TEST.ABC.COM
DS6-TESTA-APP-1.TEST.ABC.COM
DS6-TESTA-ASYNC-1.TEST.ABC.COM
DS6-TESTA-ER-1.TEST.ABC.COM
DS6-TESTA-MAA-1.TEST.ABC.COM
DS6-TESTA-MAPP-1.TEST.ABC.COM
DS6-TESTA-MAW-1.TEST.ABC.COM
DS6-TESTA-SCAN-1.TEST.ABC.COM
DS6-TESTA-SEARCH-1.TEST.ABC.COM
DS6-TESTA-SERVICE-1.TEST.ABC.COM
DS6-TESTA-TRACKER-1.TEST.ABC.COM
DS6-TESTA-WWW-1.TEST.ABC.COM
FIN-DEV-UC4DB-1.TEST.ABC.COM
MDS-DEV-APP-2.TEST.ABC.COM
MDS-DEV-DB-2.TEST.ABC.COM
MON-TEST-LOG-3.TEST.ABC.COM
MON-TEST-LOGIDX-3.TEST.ABC.COM
MOS-DEV-DB-1.TEST.ABC.COM
MCS-DEV-ARDAPP-1.TEST.ABC.COM
MCS-DEV-DB-1.TEST.ABC.COM
MCS-DEV-IDDIR-1.TEST.ABC.COM
MCS-DEV-PASSCENT-1.TEST.ABC.COM
MCS-DEV-PASSDB-1.TEST.ABC.COM
MCS-DEV-PASSPORT-1.TEST.ABC.COM
MCS-DEV-PASSPORT-2.TEST.ABC.COM
MCS-DEV-PASSPORT-3.TEST.ABC.COM
MCS-GRVDEV-DPOPS-1.TEST.ABC.COM
MON-DEV-NMS-1.TEST.ABC.COM
XORN.STAGE.XYZ.COM
JMS-DEV-FILE-1.STAGE.XYZ.COM
ROGUE.STAGE.XYZ.COM

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-05-2016 at 06:43 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-05-2016
Hi,
Just for fun (work under gnu grep):
Code:
grep -Po -- '--box=\K[^ ]*' file

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. Shell Programming and Scripting

Extract the word from the file and print it

I have a file which I am reading and then I need to extract a particualr word and if it matches the line. 2015-01-22 07:30:17,814000 +0900 /INFO: - <ns2:virtualServerid="PH11PK" /> Means if the line contain Virtual server I need to extract the id . Code I wrote#!/usr/bin/perl ... (19 Replies)
Discussion started by: karan8810
19 Replies

3. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

5. Shell Programming and Scripting

How to get the next word which falls just after a keyword?

Hi friends, i just want to know the command though which i can get the next word which comes just after a particluar keyword. For example: suppose text.out is file which contains a pl/sql procedure . i want to find out the word which falls just after the "table1" keyword. Thank... (7 Replies)
Discussion started by: neelmani
7 Replies

6. Shell Programming and Scripting

extract a word from text file name

Hi i want to extract the word present before .txt in the text file. For example, Sample_ab_a.txt ----------> i need 'a' Sample_abc_b.txt -----------> i need 'b' Can anyone help me in getting the word extracted (5 Replies)
Discussion started by: Sindhuap
5 Replies

7. UNIX for Dummies Questions & Answers

how to find a word in a file that appears next to a given keyword

Hi Experts, I have a file which contains some text. i need to print the word next to a given keyword. Please help. Ex: test.txt ===================== NEXT HOST ===================== AEADBAS001 access-list 1 permit xxxxxxxxxxxxxx ip access-list extended BLA_Outgoing_Filter... (6 Replies)
Discussion started by: mwrg
6 Replies

8. Shell Programming and Scripting

How to extract just a word from a File in Shell?

Hello Friends, I have a txt file which has data like this TNS Ping Utility for Solaris: Version 10.2.0.3.0 - Production on 23-MAR-2010 15:38:42 Copyright (c) 1997, 2006, Oracle. All rights reserved. Used parameter files: Used TNSNAMES adapter to resolve the alias Attempting to... (7 Replies)
Discussion started by: njafri
7 Replies

9. Shell Programming and Scripting

Replace only if the keyword is the first word in every line

How do I replace only if the keyword is at the begining of a line? Code: -- a = “This is a print statement” print a -- What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword. Please help me out. I'm new to SED. -----Post... (5 Replies)
Discussion started by: alexzubin
5 Replies

10. UNIX for Dummies Questions & Answers

extract last word on line to new file

Can someone please help me with how to extract the last word on a line to a new file? I have a list of names like: Ms. Nell D. Bullock Mrs. Sherrie M Avent LINDA ANNETTE RUSSELL Mr. Jerome R. Harris Pandora Tyndall I want the new file to look like this: Bullock Avent RUSSELL Harris... (10 Replies)
Discussion started by: michieka
10 Replies
Login or Register to Ask a Question