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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grab a block of data in a file with repeating pattern?
# 1  
Old 01-29-2013
Wrench 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 email. i used commands like

Code:
grep -n "TO :" Sample2.txt|cut -d ":" -f 1,3| tr -d ' '

to get line numbers with email ids.

sed -n '/HELO/,/HELO/p' Sample2.txt this is where i am unable to get the corresponding block for that email id. Smilie



The sample file is

Code:
HELLO 

MAIL FROM: <A@B.COM>                                                                                                           

RCPT TO: <RCPT1@TEMP.COM>                                                                                                      

Date: 16 JUN 11 18:28:41 EST                                                                                                         

FROM: TEMPSENDER@EMAIL.COM                                                                                                                  

TO : TEMPRECEIVER@EMAIL.COM                                                                                                     

DATA 

               ABCDCDCDCDC
               
THANK YOU,                                                                                                                           

COMPANY

.

HELLO 

MAIL FROM: <A@B.COM>                                                                                                           

RCPT TO: <RCPT2@TEMP.COM>                                                                                                      

Date: 16 JUN 11 18:28:41 EST                                                                                                         

FROM: TEMPSENDER@EMAIL.COM                                                                                                                  

TO : TEMPRECEIVER2@EMAIL.COM                                                                                                     

DATA 

               ASDASDSADASDASD
               
THANK YOU,                                                                                                                           

COMPANY


Last edited by Scott; 05-30-2013 at 02:05 PM.. Reason: Please use code tags
# 2  
Old 01-29-2013
In sed, you can read in each message up to COMPANY using 'N' and process it like a long line.
# 3  
Old 05-30-2013
Hi,

How would you do that ?
# 4  
Old 05-30-2013
You use the N to append the next line and loop back if COMPANY is not there:
Code:
sed '
  :lopp
  /COMPANY/!{
    N
    b loop
    }
  .
  .
  .
 '

The resulting buffer matches '^line1\nline2\n...lineN$'.
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 05-31-2013
Thanks, very useful.
# 6  
Old 06-05-2013
PS: Looks like it ends in ^\.$ like smtp/esmtp data, not COMPANY.
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

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 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

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

6. UNIX for Dummies Questions & Answers

Extract repeating data from file

I want to extract the last rows of a data file, similar to that one below: C1 xxx C2 rrr C3 ttt .... Cn-1 hhh Cn bbb C1 yyy C2 sss C3 uuu ... Cn-1 iii Cn ccc ... I just want to extract the final rows between C1 and Cn at each data file. n is not a constant,... (2 Replies)
Discussion started by: natasha
2 Replies

7. Shell Programming and Scripting

Remove repeating pattern from beginning of file names.

I want a shell script that will traverse a file system starting at specific path. And look at all file names for repeating sequences of and remove them from the file name. The portion of the name that gets removed has to be a repeating sequence of the same characters. So the script would... (3 Replies)
Discussion started by: z399y
3 Replies

8. Shell Programming and Scripting

Can I split a 10GB file into 1 GB sizes using my repeating data pattern

I'm not a unix guy so excuses my ignorance... I'm the database ETL guy. I'm trying to be proactive and devise a plan B for a ETL process where I expect a file 10X larger than what I process daily for a recast job. The ETL may handle it but I just don't know. This file may need to be split... (3 Replies)
Discussion started by: john091
3 Replies

9. 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

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question