How to group matched patterns in different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to group matched patterns in different files
# 1  
Old 03-14-2011
How to group matched patterns in different files

Hi,

I have a master file that i need to split into multiple files based on matched patterns. sample of my data as follows:-

Code:
scaff_1     a       e     123       130         c_scaff_100
 scaff_1     a       e     132       138         c_scaff_101
 scaff_1     a       e     140       150         c_scaff_102
 scaff_2     a       e     151       160         c_scaff_109
 scaff_2     a       e     181       200         c_scaff_110
 scaff_2     a       e     223       230         c_scaff_120
 scaff_3     a       e     323       340         c_scaff_200
 scaff_3     a       e     352       359         c_scaff_201
 scaff_3     a       e     390       430         c_scaff_203

i want the result to be generated in 3 different files (based on my input) and the output should look like this:-

scaffR1
Code:
scaff_1     a       e     123       130         c_scaff_100
scaff_1     a       e     132       138         c_scaff_101
scaff_1     a       e     140       150         c_scaff_102

scaffR2
Code:
scaff_2     a       e     151       160         c_scaff_109
scaff_2     a       e     181       200         c_scaff_110
scaff_2     a       e     223       230         c_scaff_120

scaffR3
Code:
scaff_3     a       e     323       340         c_scaff_200
scaff_3     a       e     352       359         c_scaff_201
scaff_3     a       e     390       430         c_scaff_203

i had tried 2 methods as follows:-

1)
Code:
awk '/^scaff_/{close("scaffR"f); f++}{print $0 >"scaffR" f}' data.txt

2)
Code:
grep '\bscaff_1\b' data.txt > scaffR1.txt

But both did not really give me the result as i expected. The first one save each line to a new file. the second one group them but requires me to do it manually.

would appreciate kind help on this. Thanks..Smilie
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-14-2011 at 06:54 PM.. Reason: code tags, please!
# 2  
Old 03-14-2011
Code:
nawk '{close(f);f=$1;sub("_","R",f);print $0 >> f}' date.txt

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 03-14-2011
Hi vgersh99,

It works great!!! Thanks so much for your prompt response dan kind help.... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

2. Shell Programming and Scripting

How to print two matched patterns only from each line?

My input looks like this. # Lot Of CODE Before AppType_somethinglese=$(cat << EOF AppType_test1='test-tool/blatest-tool-ear' AppType_test2='test/blabla-ear' # Lot Of CODE After I want to print text betwen 1) _ and = and 2)/ and ' from each line and exclude lines with "EOF". Output... (2 Replies)
Discussion started by: kchinnam
2 Replies

3. Shell Programming and Scripting

Extract all the sentences that matched two patterns

Hi I have two lists of patterns named A and B consisting of around 200 entries in each and I want to extract all the sentences from a big text file which match atleast one pattern from both A and B. For example, pattern list A consists of : ama ani ahum mari ... ... and pattern... (1 Reply)
Discussion started by: my_Perl
1 Replies

4. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

5. Shell Programming and Scripting

Matched multiple patterns that could be in a same line

Hi, I need help to match pattern started with "RW" in file 1 and with pattern in $1 in file 2 as follows:- File 1 BH /TOTAL=466(423); /POSITIVE=300(257); /UNKNOWN=25(25); BH /F_P=141(141); /F_N=136; /P=4; CC /TAX=!?; /MAX-R=2; CC /VER=2; RW P9610, AR_BSU , T; PAE25, AE_E57... (10 Replies)
Discussion started by: redse171
10 Replies

6. Shell Programming and Scripting

Find matched patterns in a column of 2 files with different size and merge them

Hi, i have input files like below:- input1 Name Seq_ID NewID Scores MT1 A0QZX3 1.65 277.4 IVO A0QZX3 1.65 244.5 HPO A0QZX3 1.65 240.5 RgP A0Q3PP 5.32 241.0 GX1 LPSZ3S 96.1 216.9 MEL LPSS3X 4.23 204.1 LDD LPSS3X 4.23 100.2 input2 Fac AddName NewID ... (9 Replies)
Discussion started by: redse171
9 Replies

7. Shell Programming and Scripting

Find matched patterns in multiple files

Hi, I need help to find matched patterns in 30 files residing in a folder simultaneously. All these files only contain 1 column. For example, File1 Gr_1 st-e34ss-11dd bt-wwd-fewq pt-wq02-ddpk pw-xsw17-aqpp Gr_2 srq-wy09-yyd9 sqq-fdfs-ffs9 Gr_3 etas-qqa-dfw ddw-ppls-qqw... (10 Replies)
Discussion started by: redse171
10 Replies

8. Shell Programming and Scripting

Delete lines and the first pattern between 2 matched patterns

Hi, i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows: inputfile.txt >kump_1 ........................... ........................... >start_0124 dgfhghgfh fgfdgfh fdgfdh >kump_2 ............................. (7 Replies)
Discussion started by: redse171
7 Replies

9. Shell Programming and Scripting

Grab contents between two matched patterns

I am wanting to fetch the content of the table within a file the table begins with data label like N Batch Mn(I) RMSdev I/rms Rmerge Number Nrej Cm%poss AnoCmp MaxRes CMlplc SmRmerge SmMaxRes $$ $$ . #columns of data . . . . . $$ I tried the command awk... (18 Replies)
Discussion started by: piynik
18 Replies

10. UNIX for Dummies Questions & Answers

grep to show patterns being matched (-f option)

I have a list of patterns (regexes) in a file and use with `grep -f <file_with_list_of_regexes.txt> input.txt` to search in my input for those patterns. grep is doing a fantastic job at it and finds me the matching input text but I also want to see in the output the regex (from... (1 Reply)
Discussion started by: mirage
1 Replies
Login or Register to Ask a Question