How to grab data between 2 strings ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grab data between 2 strings ?
# 8  
Old 10-05-2006
Do you always get

xxxx
yyy
zzz
blah
blah

i.e. 5 lines or does the number vary ?
# 9  
Old 10-05-2006
Quote:
Originally Posted by vino
Do you always get

xxxx
yyy
zzz
blah
blah

i.e. 5 lines or does the number vary ?
Hi Vino,

The number of lines do vary
# 10  
Old 10-05-2006
Quote:
Originally Posted by Raynon
** 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
************************************


You get it twice because the same pattern occurs in the file 2 times. Will there be any difference in the pattern occurences?
# 11  
Old 10-05-2006
Quote:
Originally Posted by vish_indian
You get it twice because the same pattern occurs in the file 2 times. Will there be any difference in the pattern occurences?
Hi Vish,

I understand now. So if there's a difference in the pattern, it will output my desired output. Thanks.

Could you pls explain your code in details because i;m not familiar with sed ?
# 12  
Old 10-05-2006
Code:
sed -n '/05T00NPQSMR1/,/*\{58\}/p' input

-n
By default SED will print all the lines it process . But we need only certain parts of the file . Use -n option to tell SED not to print lines by default.

/05T00NPQSMR1/,/*\{58\}/
Here we are specifying the range of address we need.
This will match from any line containing pattern 05T00NPQSMR1 till the line contains 58 asterisks.

p
p command is used to print any line that is matched by the above regular expression.
# 13  
Old 10-05-2006
Code:
awk -v pat1="05T00NPQSMR1" -v pat2="^[*]{3,}" '$0~pat1,$0~pat2' awtest

-v is used to pass the shell variables to awk.

Work in the above code is done by $0~pat1,$0~pat2. $0 in awk means the line that was read from the file. There are actions and patterns in awk, for a matching pattern specified action is performed.

In this code, data between pattern1("05T00NPQSMR1") and pattern2([*]{3,} - 3 0r more *s) is selected. no action is specified as default action is to print the matched lines.

Please see man awk for more details.
# 14  
Old 10-05-2006
Quote:
Originally Posted by vish_indian
Code:
awk -v pat1="05T00NPQSMR1" -v pat2="^[*]{3,}" '$0~pat1,$0~pat2' awtest

-v is used to pass the shell variables to awk.

Work in the above code is done by $0~pat1,$0~pat2. $0 in awk means the line that was read from the file. There are actions and patterns in awk, for a matching pattern specified action is performed.

In this code, data between pattern1("05T00NPQSMR1") and pattern2([*]{3,} - 3 0r more *s) is selected. no action is specified as default action is to print the matched lines.

Please see man awk for more details.
pat2="^[*]{3,}"
Hi Vish,
Why does "^" and "[]" and "{}" symbolise in the code ?

In $0~pat1,$0~pat2, what does the "~" means ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY. I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next... (5 Replies)
Discussion started by: loggedout
5 Replies

3. Shell Programming and Scripting

Grab data within a table in a long log file.

in my file which is a rather long log file it contains many text and tables and there is one table with 15 columns and I am interested to read in the value in column6 and its corresponding value in column2. Trouble is I do not know how to script it as the line number various between different log... (8 Replies)
Discussion started by: piynik
8 Replies

4. Shell Programming and Scripting

Grab 2 pieces of data within a file

I am a newbie and what I have is a captured file of content. I want to be able to grab 2 pieces of data, multiple times and print them to the screen. DataFile owner: locke user: fun data size: 60 location: Anaheim owner: david user: work data size: 80 location: Orange my script... (2 Replies)
Discussion started by: greglocke
2 Replies

5. Shell Programming and Scripting

Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus, I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword. for eg: I need to search between Test = 000; and Test = 000; and find K9 and replace with M9 INPUT FILE Define { Replace = K9; Test =... (6 Replies)
Discussion started by: naveen@
6 Replies

6. Shell Programming and Scripting

How to grab data from xml block?

I tried searching the forums, but couldn't find anything relevant to my question. I have an xml file like the following: <topLevel numberBlock="BLOCK1"> <item="content1" title="Content 1"> <RefPath="path/to/file1.txt /> </item> <item"content2" title="Content 2" >... (4 Replies)
Discussion started by: jl487
4 Replies

7. Shell Programming and Scripting

Grab the data

Hello Honourable Members, I stuck into one issue, my server is migrating from UNIX to linux and ptree command does not work there. I was working with pstree command in linux and need some help regarding the same. suppose i have one line for example: ram (121)--- sita... (3 Replies)
Discussion started by: singhabm
3 Replies

8. Shell Programming and Scripting

Using Python to grab data from a website

Hello Everyone, I'm trying to write a python script that will go to the following website and grab all the data on the page. The page refreshes regularly and the number of flights is different. Untitled Document What I wanted to do was grab all the data (except for top three row containing... (5 Replies)
Discussion started by: jl487
5 Replies

9. UNIX for Dummies Questions & Answers

grab the data from the unix window

Hi, How could i grab a set of data (eg:file execution start & stop time stamp f) from unix? (1 Reply)
Discussion started by: siriv
1 Replies

10. UNIX for Dummies Questions & Answers

search and grab data from a huge file

folks, In my working directory, there a multiple large files which only contain one line in the file. The line is too long to use "grep", so any help? For example, if I want to find if these files contain a string like "93849", what command I should use? Also, there is oder_id number... (1 Reply)
Discussion started by: ting123
1 Replies
Login or Register to Ask a Question