Spliting file based field pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spliting file based field pattern
# 1  
Old 06-30-2010
Spliting file based field pattern

Hi all,

i have file that looks like as below

Code:
2263881188,24570896,439,SOLO,SOLO_UNBEATABLE,E,+3.13,+0.00
2263881964,24339077,439,SOLO,SOLO_UNBEATABLE,F,-0.67,+0.00
2263883220,22619162,228,Bell,Bell_MONTHLY,E,-2.04,+0.00
2263883220,22619162,228,Bell,Bell_MONTHLY,F,-2.04,+0.00
2267475054,23627758,213,Bell,Bell_BB,F,-6.89,+0.00
2267477589,22625847,230,Bell,Bell_BB,F,-7.31,+0.00

i have huge reocrd like this in file say file1

i want to split this file with pattern say SOLO_UNBEATABLE,E into one file say SOLO_UNBEATABLE_E

SOLO_UNBEATABLE,F into other file SOLO_UNBEATABLE_F

and Bell_MONTHLY,E into seperate file Bell_MONTHLY_E and so on

all the matched pattern records should into corresponding file.

Please let me know if any awk script to do that ..
# 2  
Old 06-30-2010
Code:
awk -F"," -v OFS="," ' { print > $5"_"$6 } ' file

# 3  
Old 06-30-2010
Thanks a lot ! Smilie
# 4  
Old 06-30-2010
Code:
#!/usr/bin/perl
use strict;
use warnings;

my ($file, $i, @line);

$file="file.txt";

open(FILE,"<",$file, $file);
foreach $i (<FILE>){
	@line=split(/,/,$i);
	open(FILESPLIT,">>","$line[4]_$line[5]");
	print FILESPLIT "@line\n";
	close(FILESPLIT);
}
close(FILE);

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 look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

2. Shell Programming and Scripting

Find pattern in first field of file

Hello all I have two files. 1. Pattern.txt - It contains patterns to be matched. It has large number of patterns to be matched. Cat Pattern.txt Ram Shyam Mohan Jhon I have another file which has actual data and records are delimted by single or multiple spaces. 2.... (8 Replies)
Discussion started by: krsnadasa
8 Replies

3. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

4. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

5. UNIX for Dummies Questions & Answers

redirect to a file based on a value in a field

Hi all! Is it possible with awk to redirect all the records of a file that match a string in a field to a specific file. input.tab: aaaaa|bbbbbb|KKKKK cccccc|dddddd|SSSSS eeeeee|ffffffff|SSSSS ggggg|hhhhhh|KKKKK redirected to 2 separate files depending on the value in $3: ... (3 Replies)
Discussion started by: lucasvs
3 Replies

6. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

7. Shell Programming and Scripting

Split file based on field

Hi I have a large file 2.6 million records and I am trying to split the file based on last column. I am doing awk -F"|" '{ print > $NF }' filename1 After around 1000 splits it gives me a error awk: can't open file 3332332423 input record number 1068, file filename1 source... (6 Replies)
Discussion started by: s_adu
6 Replies

8. Shell Programming and Scripting

Spliting file based on condition

Hi, I have a comma separated file with millions of records in it. I have a requirement to split the file based on the value in a one of the columns. Suppose i have a text file with columns like C1, C2,C3,C4 Column C4 can hold the values either 01 or 02 03 or 04. I nned to extract... (2 Replies)
Discussion started by: Raamc
2 Replies

9. Shell Programming and Scripting

Spliting the line based on position.

i want to split a big line based on the position. example : I have a single line which has 2300 characters. i want to split from 1 character to 300th characters as first line and 301th to 600 as second line and 601th to 900 as third line ...till the end of the string. Can anyone help... (1 Reply)
Discussion started by: senthil_is
1 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question