to break a file into 2 files after matching a pattern.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers to break a file into 2 files after matching a pattern.
# 1  
Old 04-01-2009
to break a file into 2 files after matching a pattern.

Hi,
i need to break a file into 2 files afetr matching a pattern

for ex. there is a fil, file .txt which contains

Quote:
brett & vivs RM03PE00481630390046 0018H00000421973G D6C
BC7116379A00607-1 3000023237098964413 00000000A000000001199100000116310000000036{D ALPHA beta CS D7C

Mat & Demon RM03PE00481630390046 0018H00000421973G D6C
BA8007-1 300002323963164 000000000B000000001199000116310000000072{D ALPHA beta D7C

BC71160607-1 3000024456898962294 000000000A000000007612500073841000000000000228DD ALPHA beta CS D7C

john & tom RM03PE00481630390046 0018H00000421973G D6C
BA8007-1 300002323963164 000000000B000000001199000116310000000072{D ALPHA beta D7C
here i need to look for mat $ demon if it matches then i need to transfer the data into another file till the line in which a "d6s" comes,and i have to delete tat line from the file and copy the remaining data in another file
like
file 1
Quote:
Mat & Demon RM03PE00481630390046 0018H00000421973G D6C
BA8007-1 300002323963164 000000000B000000001199000116310000000072{D ALPHA beta D7C

BC71160607-1 3000024456898962294 000000000A000000007612500073841000000000000228DD ALPHA beta CS D7C
and file 2
Quote:
brett & vivs RM03PE00481630390046 0018H00000421973G D6C
BC7116379A00607-1 3000023237098964413 00000000A000000001199100000116310000000036{D ALPHA beta CS
john & tom RM03PE00481630390046 0018H00000421973G D6C
BA8007-1 300002323963164 000000000B000000001199000116310000000072{D ALPHA beta D7C
thanks in advance

Last edited by manit; 04-01-2009 at 07:28 AM..
# 2  
Old 04-01-2009
Awk

Awk can do this.

Set a flag when you match the first pattern, clear it when you find the second.

Something like this might help you get started

Code:
#!/usr/bin/awk -f

BEGIN {
   flag = 0
   quiet = 0
}

/onpattern/ {
   flag = 1
   quiet = 1
}

/offpattern/ {
   flag = 1
   quiet = 1
}

{
   if (! quiet) {
      if (flag) {
         print $0 >> "on.txt"
      } else {
         print $0 >> "off.txt"
      }
   } else {
      quiet = 0
   }
}

When flag is set, it redirects lines to "on.txt". When it's reset, it redirects lines to "off.txt". The quiet flag is there in case you don't want the onpattern and offpattern lines to make it to the output files. I think in your case, you DO want them to be copied, but I added them to the program just in case. If you DO want them to be copied, just remove all the logic around quiet, or never set it to 1 in the onpattern and offpattern cases.

HTH

Last edited by Franklin52; 04-01-2009 at 10:45 AM.. Reason: adding code tags
# 3  
Old 04-01-2009
Or shorter:

Code:
awk '
p && /&/{p=0}
/Mat & Demon/{p=1}
p{print > "file1";next}
{print}' file.txt > file2

Regards
# 4  
Old 04-01-2009
Thanks for replying
actualy i was using the command
Quote:
sed -n '/Mat & Demon /,/D6C/ p' <input_file
but what will i do in case of multiple occurence of [mat $ demon to d6s]
Thnax in advance
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

2. Shell Programming and Scripting

Help with Pattern Matching and replacement in Gz files

Hi Techies, I need a help in finding junk characters and remove them from a Datafile. we have a file and it had crores of records like below SGSN_MCC_MNC=01150 but sometime due to the issue with sending server we are getting some junk characters in the middle of data like below ... (6 Replies)
Discussion started by: mahi_mayu069
6 Replies

3. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

4. Shell Programming and Scripting

Removing files matching a pattern

I am on ubuntu 11.10 using bash scripts I want to remove all files matching a string pattern and I am using the following code find . -name "*$pattern*" -exec rm -f {} \;I have encountered a problem when $pattern is empty. In this case all my files in my current directory were deleted. This... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

6. Shell Programming and Scripting

To find files by matching a pattern in file name

Hi all, I have to check whether certain files exist using a if statement. I have to check this by matching a pattern of filename: e.g. if ] This statement should be "true" if any files like test.dat11, test.dat22 etc are present in the source dir. However, this statement is checking only... (2 Replies)
Discussion started by: sweety123
2 Replies

7. Shell Programming and Scripting

How can i break a text file into parts that occur between a specific pattern

How can i break a text file into parts that occur between a specific pattern? I have text file having various xml many tags like which starts with the tag "<?xml version="1.0" encoding="utf-8"?>" . I have to break the whole file into several xmls by looking for the above pattern. All the... (9 Replies)
Discussion started by: abhinav192
9 Replies

8. UNIX for Dummies Questions & Answers

find files NOT matching name pattern

Hi, I have following files in my directory: /TESTDONTDEL> ls -alt total 14 drwxr-xr-x 2 oracle dba 1024 May 15 06:30 . -rw-r--r-- 1 oracle dba 40 May 15 06:30 exception.txt -rw-r--r-- 1 oracle dba 19 May 15 06:22 ful_1234_test1.txt -rw-r--r-- 1... (2 Replies)
Discussion started by: sagarparadkar
2 Replies

9. UNIX for Dummies Questions & Answers

Find files matching a pattern

Hi, I am writing a BASH shell script. I would like to count all the files in the CURRENT directory matching a specific pattern. Could someone suggest the best/simplest way to do this. I have thought of these solutions (for simplicity the pattern is all files starting with A): ls -1 *A | wc -l... (5 Replies)
Discussion started by: msb65
5 Replies

10. UNIX for Dummies Questions & Answers

rm core files and pattern matching

Hi, I am trying to delete a load of core files, but make sure I only delete core files. The system I am using has many files with core in the name, so I obviously can not simply search for "core". I have tried using the 'find' command with pattern matching via , and know that his is the way... (3 Replies)
Discussion started by: littleIdiot
3 Replies
Login or Register to Ask a Question