Extract word between two KEYWORDS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract word between two KEYWORDS
# 1  
Old 07-18-2013
Extract word between two KEYWORDS

Hi
I want to extract all the words between two keywords HELLO & BYE.

eg:
Input
Code:
1_HELLO_HOW_ARE_YOU_BYE_TEST
1_HELLO_WHERE_ARE_BYE_TEST
1_HELLO_HOW_BYE_TEST

Output Required:
Code:
HOW_ARE_YOU
WHERE_ARE
HOW


Last edited by Scott; 07-18-2013 at 05:32 AM.. Reason: Code tags
# 2  
Old 07-18-2013
Code:
awk -F"HELLO_" '{print $2}' filename | awk -F"_BYE" '{print $1}'

This User Gave Thanks to krishmaths For This Post:
# 3  
Old 07-18-2013
Thanks...
However I have to implement this in PERL script.
So when I am using it like

Code:
$var1 = `awk -F"HELLO_" '{print $2}' filename | awk -F"_BYE" '{print $1}'`

I am getting error:

Code:
Use of uninitialized value $2 in concatenation (.) or string.
Use of uninitialized value $1 in concatenation (.) or string.

Kindly assist.

Last edited by Scott; 07-18-2013 at 06:06 AM.. Reason: Use code tags, please...
# 4  
Old 07-18-2013
You can use below code in sed

Code:
 
sed 's/1_HELLO_\(.*\)_BYE\(.*\)/\1/g' filename

Code:
 
bash-3.00$ sed 's/1_HELLO_\(.*\)_BYE\(.*\)/\1/g' a
HOW_ARE_YOU
WHERE_ARE
HOW

# 5  
Old 07-18-2013
Code:
$ perl -pe 's/.+HELLO_(.+)_BYE.+/\1/' file
HOW_ARE_YOU
WHERE_ARE
HOW

# 6  
Old 07-18-2013
Hi Rajamadhavan

Thanks for prompt reply.
How to assign the output to a variable in PERL ? [Assuming I will get only one line]
# 7  
Old 07-18-2013
Another way with assigning to variable

Code:
open(FILE, "<file") || die;
my $var;
while(<FILE>) {
      $_ =~ m/.+HELLO_(.+)_BYE.+/g;  
      $var = $1;
      print $var;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract words before and after a certain word.

I have a sample text file with file name: sample.txt The text file has the following text. this is an example text where we have to extract certain words before and after certain word these words can be used later to get more information I want to extract n (a constant) words before and... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

2. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

3. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

4. Shell Programming and Scripting

Extract a word from sentence

$SET_PARAMS='-param Run_Type_Parm=Month -param Portfolio_Parm="997" -param From_Date_Parm="2011-08-09"' Want to extract the value of "Portfolio_Parm" from $SET_PARAMS i.e in the above case "997" & assigned to new variable. The existence order of "Portfolio"Parm" can change, but the name... (2 Replies)
Discussion started by: SujeethP
2 Replies

5. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

6. Shell Programming and Scripting

Extract word from a line

Hi, I've searched the forum to get what I wanted but to no avail. Here's the problem I'm facing. The line is suppose as below: <INPUT DATABASE ="ORACLE" DBNAME ="UNIX" NAME ="FACT_TABLE" OWNERNAME ="DIPS"> Now I want to extract only FACT_TABLE. The trials are as follows: awk... (3 Replies)
Discussion started by: dips_ag
3 Replies

7. Shell Programming and Scripting

Extract word using sed

Hello, I am new to sed and am trying to extract a word using sed. for example i have a line "const TotalAmount& getTotalAmount() const; " in the file test.txt I am trying to extract getTotalAmount() from the line. For this i tried cat test.txt | sed -n 's/.*get*\(\)//p But... (8 Replies)
Discussion started by: prasbala
8 Replies

8. UNIX for Dummies Questions & Answers

Word extract from a line

In a file let a.txt, has 1 line code i.e. code QC:Tiger:10: /code I need to get my output like "Tiger 10". So how can i do this? (3 Replies)
Discussion started by: anupdas
3 Replies

9. Shell Programming and Scripting

extract numbers from a word

Hi ppl, I am a bit lost on this...can some one assist. I know this can be down with awk or sed, but i cant get the exact syntax right. I need to only extract the numbers from a signle word ( eg abcd.123.xyz ) How can i extract 123 only ? Thanks (14 Replies)
Discussion started by: systemali
14 Replies

10. AIX

extract same word from two files

file1 contains: 3 file2 contains: 1 2 3 . . 20000 cat file1 | while read line do grep -s $line file2 >> file3 done my result file3 shows: 3 3 3 (4 Replies)
Discussion started by: tjmannonline
4 Replies
Login or Register to Ask a Question