![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 ************************************ |
|
||||
|
Quote:
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 |
|
||||
|
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.. |
|
||||
|
Quote:
Code:
sed -n '/05T00NPQSMR1/,/*\{58\}/p' input
|
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|