data break split


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting data break split
# 1  
Old 11-06-2008
data break split

I am trying to figure out how to split a file when the data in the new line is different from the current line using a shell script?

For eg..

if my input file contains the following

2341123 ABCAD
2341123 ANCAED
2341123 AVADV
3343434 ASDVAV
3343434 ASDFADF
4231232 ADACVAV
4231232 ADADFADFADFASDF
4343433 ASDGAV
4343433 ADFDFDF

I would like to have 4 files

file 1 should have
2341123 ABCAD
2341123 ANCAED
2341123 AVADV

file 2 should have
3343434 ASDVAV
3343434 ASDFADF

file 3 should have
4231232 ADACVAV
4231232 ADADFADFADFASDF

file 4 should have
4343433 ASDGAV
4343433 ADFDFDF

Is this possible? I am new to the unix world and need help. Any help is much appreciated and thank you in advance.
# 2  
Old 11-06-2008
You can use the awk/nawk command to read the file. Save each different key to test to a compare variable. Then compare $1 to the compare value. If they are different then change your output to point to the next file.

awk -F" " '
BEGIN { KEY=""; NUMBER=0;}
{
if ( $1 == KEY )
{
print $0 >> FILENAME;
}
else
{
NUMBER+=1;
KEY=$1;
FILENAME="file"NUMBER;
print $0 >> FILENAME;
}
NEXT
}' myfile

result is
sh-3.2$ cat file1
2341123 ABCAD
2341123 ANCAED
2341123 AVADV

sh-3.2$ cat file2
3343434 ASDVAV
3343434 ASDFADF

sh-3.2$ cat file3
4231232 ADACVAV
4231232 ADADFADFADFASDF

sh-3.2$ cat file4
4343433 ASDGAV
4343433 ADFDFDF
sh-3.2$
# 3  
Old 11-06-2008
Code:
awk '{f="file_"$1;print > f}' file

# 4  
Old 11-07-2008
Thank you

Thank you so much.. That worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split JSON to different data files

Hi Gurus, I have below JSON file, now I want to rewrite this file into a new file. I will appreciate if anyone can help me to provide the solution...I can't use jq. { "_id": "3ad893cb4cf1560add7b4caffd4b6126", "_rev": "1-1f0ce165e1d210319cf6e9f9c6ff654f", "name":... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

2. Shell Programming and Scripting

Split data into multiple lines

Hi, if i have data like below: Control|AC-00011-CN-2475208 AC-00011-CN-2475211 AC-00007-CN-2475238 AC-00007-CN-2475241 Im getting output in required format as below Control|AC-00011-CN-2475208 Control|AC-00011-CN-2475211 Control|AC-00007-CN-2475238 Control|AC-00007-CN-2475241 using awk... (9 Replies)
Discussion started by: JSKOBS
9 Replies

3. Shell Programming and Scripting

Split a file into several files using a data

Hi All, I have file(File1) with data like below: 102100|LName|Gender|Company|Branch|Bday|Salary|Age 102100|bbbb|male|cccc|dddd|19900814|15000|20| 102101|asdg|male|gggg|ksgu|19911216||| 102102|bdbm|male|kkkk|acke|19931018||23| 102102|kfjg|male|kkkc|gkgg|19921213|14000|24|... (2 Replies)
Discussion started by: sarav.shan
2 Replies

4. Shell Programming and Scripting

split data by line

I would like break in two line by 'SNAG' Current data: SNAG|M1299063| | | | |0001.|0010.|AC64797|2008-02-18|093730.|YVR|AC64797|2008-02-18-09.37.30.250020|N|30|NO LEAKS OR CRACKS THIS A7 SCK SNAG|M1299063| | | |... (10 Replies)
Discussion started by: javeiregh
10 Replies

5. Shell Programming and Scripting

How to split a data assigned to a variable

The requirement is, there is a log file which contains a huge data. i need to get a particular field out of it by searching with another field. ex: 2011-03-28 13:00:07,423 : millis=231 q={ call get_data_account(?,?,?,?,?) }, params= i need to search for the word "get_data_account" in file... (1 Reply)
Discussion started by: Jassz
1 Replies

6. Shell Programming and Scripting

Split Date/time data to two

Hi all, I have logs(in a log file) with the following structure 20100916011501559;0.812;null;TRUE;;FALSE;0.812;0;0;;19 20100916011504762;0.015;null;TRUE;;FALSE;0;4|4;0.015;;4 20100916011504762;0;null;TRUE;;FALSE;0;0;0;;4 20100916011501731;3.343;null;TRUE;;FALSE;3.156;131|65;0.172;;11... (14 Replies)
Discussion started by: babai
14 Replies

7. Shell Programming and Scripting

Split file by data group

Hi all, I'm having a little trouble solving a file split I need to get done. I have the following data: 1. Light 1A. Light Soft texture: it's soft color: the color value is that of something light vital statistics: srm: 23 og: 1.035 sp: 1.065 comment: this is nice if you like... (8 Replies)
Discussion started by: mkastin
8 Replies

8. Shell Programming and Scripting

Split a huge data into few different files?!

Input file data contents: >seq_1 MSNQSPPQSQRPGHSHSHSHSHAGLASSTSSHSNPSANASYNLNGPRTGGDQRYRASVDA >seq_2 AGAAGRGWGRDVTAAASPNPRNGGGRPASDLLSVGNAGGQASFASPETIDRWFEDLQHYE >seq_3 ATLEEMAAASLDANFKEELSAIEQWFRVLSEAERTAALYSLLQSSTQVQMRFFVTVLQQM ARADPITALLSPANPGQASMEAQMDAKLAAMGLKSPASPAVRQYARQSLSGDTYLSPHSA... (7 Replies)
Discussion started by: patrick87
7 Replies

9. Shell Programming and Scripting

To Break data out of HTML

I'm working with the output of an html form and trying to get it into CSV. The html is a table with many entries like the following. <tr><td nowrap><b><font size=3>NAME</font></b></td><td nowrap><b>License # : </b>&nbsp;LICENSE</td></tr> <tr><td><b>City : </b>&nbsp;CITY<td nowrap><b>Type :... (1 Reply)
Discussion started by: phip
1 Replies

10. UNIX for Dummies Questions & Answers

Split data into multiple lines

All, I have a requirement where I will need to split a line into multiple lines. Ex: Input: 2ABCDEFGH2POIYUY2ASDGGF2QWERTY Output: 2ABCDEFGH 2POIYUY 2ASDGGF 2QWERTY The data is of no fixed lenght. Only the lines have to start with 2. How can this be done. (5 Replies)
Discussion started by: kingofprussia
5 Replies
Login or Register to Ask a Question