Separating Pattern Into Separate Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separating Pattern Into Separate Files
# 1  
Old 09-26-2010
Separating Pattern Into Separate Files

I am trying to separate a specific pattern match into separate files. Sometimes there is only one pattern match, but other times there could be multiple (up to 6 or 8). Pattern is as follows - its starts with NYZ[0-9][0-9][0-9] or VTZ[0-9][0-9][0-9] and ends with $$. Again looking to get those blocks of data from one big file into separate files. Awk would be ideal...thanks a million!
Code:
NYZ[0-9][0-9][0-9]-123456-
...data...
...data...
...data...
 
$$

Thanks again for the help!

Last edited by Franklin52; 09-27-2010 at 03:27 AM.. Reason: code tags
# 2  
Old 09-26-2010
Code:
$ ruby -00 -ne 'd=$_.split("$$");d.each_with_index{|x,y| open("o#{$.}.txt","w"){|f|f<<x} if x=~/^(VTZ|NYZ)\d{3}/} ' file

or
Code:
$ ruby -ne 'BEGIN{$/="$$"}; open("o#{$.}.txt","w"){|f|f<<$_} if $_=~/^(VTZ|NYZ)\d{3}/ ' file

# 3  
Old 09-26-2010
Use code tags in this forum.

it will be better, if you can provide the sample input and expect output.

For my guess, you have some datas as below:

Code:
NYZ123-123456-
...data...
...data...
...data...

$$

VTZ456-123456-
...data...
...data...
...data...

$$

And you need export the NYZ or VTZ block to different files.

Try this:

Code:
awk '
/^(NYZ|VTZ)[0-9]+/ {a=substr($0,1,3);b=1} 
/\$\$/ {print >a;b=0} 
{if (b==1) print >a}
' infile

it will genereate two files: VTZ, NYZ

Code:
$ cat VTZ
VTZ456-123456-
...data...
...data...
...data...

$$

$ cat NYZ
NYZ123-123456-
...data...
...data...
...data...

$$

# 4  
Old 09-26-2010
Thanks for the help. Yes I will try and make the code look better. Actually all of this is just text data. Here is what I have:
Code:
NYZ026-029-271600-
...data...
...data...
...data...
 
$$
 
VTZ001-005-272000-
...data...
...data...
...data...
 
$$
 
NYZ034-272000-
...data...
...data...
...data...
 
$$

The numbers immediately after NYZ and VTZ are always different, so trying to capture a particular sequence may not be best. But I am new to splitting chunks of data into smaller files. Each of these pieces of data would need to go into its own file and not lumped together as everything that starts with VTZ and NYZ. I hope this clarifies things a bit. Plus I do not know what Ruby is...thanks again for the help!


Moderator's Comments:
Mod Comment Please use code tags

Last edited by Franklin52; 09-27-2010 at 03:44 AM.. Reason: Changed first line to add the fact that this is text data.
# 5  
Old 09-27-2010
No test of the '$$' pattern.
shell code:
  1. while read L
  2. do
  3.    N=$(echo $L | grep -E '^(VTZ|NYZ)[0-9]+') && { F=$N; :>$F; }
  4.    echo $L >> $F
  5. done < infile
# 6  
Old 09-28-2010
Okay, I just read how to do code tags, so here is the text file with code tags. Frans, when I tried your code, it gave me while: Expression Syntax. Any help? Thanks!

NYZ026-029-271600-
There is text data here.
And more text data here.
And even more text data here.

$$

VTZ001-002-271600-
There is text data here.
And more text data here.
And even more text data here.

$$

NYZ034-271600-
There is text data here.
And more text data here.
And even more text data here.

$$
# 7  
Old 09-28-2010
What's your shell ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Pattern match and write to separate files

I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2). Mapping for pattern match and add line : for Alpha 123 for Beta 234 for Gamma 345 no match (goes into another file) File 1 ... (3 Replies)
Discussion started by: w020637
3 Replies

2. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

3. Programming

Separating two classes in two files

I have a file Map.hh shown below. I want to put the two classes Phase and Map in two different files Phase.hh and Map.hh. I have forward declaration before the Map class. How can I tackle this situation? ////////////////////////////////////////////////////////////////////////// #ifndef... (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

5. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

6. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

7. Shell Programming and Scripting

Merging files into a single tab delimited file with a space separating

I have a folder that contains say 50 files in a sequential order: cdf_1.txt cdf_2.txt cdf_3.txt cdf_3.txt . . . cdf_50.txt. I need to merge these files in the same order into a single tab delimited file. I used the following shell script: for x in {1..50}; do cat cdf_${x}.txt >>... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

8. UNIX for Dummies Questions & Answers

Simple script for separating files

Hi all, I was wondering if someone could help me writing a simple script on separating files into separate folders. I have 92 files called various things and I want to separate these folders like so: Create a folder called "1" and put files 1-23 in it Create a folder called "2" and put... (5 Replies)
Discussion started by: hertingm
5 Replies

9. Shell Programming and Scripting

extract x lines after a pattern - place each result in separate file

Hi all, I have many files that have 1 or more occurrences of the information I want. There are two distinct sets of information. I want get this info and place each occurrence in its own file. The 3 lines before one set are this grid 00 01 02 16 17 18 **40 lines of code I want to... (5 Replies)
Discussion started by: gobi
5 Replies

10. Shell Programming and Scripting

to separate a specific pattern

Hi friends, I have a file with following pattern. OBJECT TYPE="locale", NAME=locale_nl_NL, UNIQUE_FIELD=win32_lcid FIELDS clarify_lang = 0; END_FIELDS END_OBJECT NAME=locale_nl_NL OBJECT TYPE="locale", NAME=locale_nl_BE UNIQUE_FIELD=win32_lcid FIELDS iso_cntry =... (11 Replies)
Discussion started by: shriashishpatil
11 Replies
Login or Register to Ask a Question