The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grab a smaller and larger value Raynon Shell Programming and Scripting 3 10-11-2007 06:23 AM
grab the line using awk cdfd123 Shell Programming and Scripting 1 10-10-2007 08:21 AM
search and grab data from a huge file ting123 UNIX for Dummies Questions & Answers 1 06-06-2006 09:41 PM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 11:58 AM
How to Grab the latest file n9ninchd UNIX for Dummies Questions & Answers 1 05-10-2001 04:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2006
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
How to grab data between 2 strings ?

Hi All,
I have a text file below. How do i grab all the data between "05T00NPQSMR1" and "****" using awk ? Pls note that the text lines may not be fixed and text content is dynamic. Pls help. Thanks

Below is my code where $LOT_SUFFIX is my shell variable.

awk '/'"$LOT_SUFFIX"'/,/blah/' $nfile_selected

Data Given:

blah
xxx
yyy
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxx
yyy
zzz
blah
blah
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************


Expected Output:
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxx
yyy
zzz
blah
blah
************************************
  #2 (permalink)  
Old 10-04-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Try this.
Code:
sed -n -e '/05T00NPQSMR1/,/^\**/p' input.txt
Else this.
Code:
sed -n -e '/05T00NPQSMR1/,/^[*]*/p' input.txt
At the moment, I dont have access to a unix terminal to test this.
  #3 (permalink)  
Old 10-04-2006
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Quote:
Originally Posted by vino
Try this.
Code:
sed -n -e '/05T00NPQSMR1/,/^\**/p' input.txt

Else this.
Code:
sed -n -e '/05T00NPQSMR1/,/^[*]*/p' input.txt
At the moment, I dont have access to a unix terminal to test this.
Hi, the output seems far from what i wanted. See below.

Output:
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
  #4 (permalink)  
Old 10-04-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
Alternative in Python:

Input:
blah
xxx
yyy
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxx
yyy
zzz
blah
blah
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxasdfljsdlfx
yyy;slkfd;s
zzzsdklfjsd
blasdflksdjh
blajaspofkspodf



Code:
number = []
all = open("test.txt").readlines()
for num,line in enumerate(all):
 	line = line.strip()
 	if "*" * 58 in line:
 		number.append(num)
 

for i in range(len(number)):
 	try:
 		print ''.join(all[ number[i]:number[i+1] ] )
 	except:
 		print ''.join(all[number[i]:])

Output:
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxx
yyy
zzz
blah
blah

**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxasdfljsdlfx
yyy;slkfd;s
zzzsdklfjsd
blasdflksdjh
blajaspofkspodf

Last edited by ghostdog74; 10-05-2006 at 03:34 AM..
  #5 (permalink)  
Old 10-04-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
Code:
awk -v pat1="05T00NPQSMR1" -v pat2="^[*]{3,}" '$0~pat1,$0~pat2' awtest
If you need data only till "** tasC"

Code:
awk -v pat1="05T00NPQSMR1" -v pat2="^** tasC" '$0~pat1,$0~pat2' awtest
  #6 (permalink)  
Old 10-04-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
blah
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
Code:
sed -n '/05T00NPQSMR1/,/*\{58\}/p' input
where 58 is length of above asterisk line
  #7 (permalink)  
Old 10-05-2006
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi Anbu,
Below code:
awk -v pat1="05T00NPQSMR1" -v pat2="^[*]{3,}" '$0~pat1,$0~pat2' awtest

& Vish,
Below code:
sed -n '/05T00NPQSMR1/,/*\{58\}/p' input

Both outputs are:
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxx
yyy
zzz
blah
blah
**********************************************************
** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************


But what i need is :

** 05T00NPQSMR1 P98P2234 Tue 10 03 09:57:24 2006
** dibid= 1166 testerid= 6
** sample= 100 0
** tasM= MFMM xx xx xx
** tasC= MFMC xx xx xx
************************************
xxxx
yyy
zzz
blah
blah


Pls help. Thanks

Last edited by Raynon; 10-05-2006 at 03:21 AM..
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0