How to split file based on subtitle


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to split file based on subtitle
# 1  
Old 06-18-2011
How to split file based on subtitle

Hi, unix Gurus,
I want to split file based on sub_title. for example:
Code:
original file
fruit
apple
watermelon
meat
pork
fish
beef

expected result file
Code:
file1
fruit
apple
watermelon

Code:
file2
meat
pork
fish

beef.
Smilie

Thanks in advance
ken002
# 2  
Old 06-18-2011
This question is like:
"I want to split binary string based on binary number. For example 011000101010110101. I want 01100010 and 1010110101"
# 3  
Old 06-18-2011
I think the point here is not unix-skills.
In your "original file", the content must be "well sorted". e.g.
Code:
fruit
fruit1
fruit2
...
fruitn
meat
meat1
meat2
...
meatn
vegetable
vegetable1
vegetable2
...
vegetablen
...

you need to give a keyword/category list, it could be a file, that defines the different categories by name("fruit", "meat", "vegetable")

then awk can split your original file as your requirement easily.
# 4  
Old 06-18-2011
i m not clearly but i try something related split..
Code:
# ./justdoit2 file 3
Showing 'numbered 'file1' text file ..
fruit
apple
watermelon
 
Showing 'numbered 'file2' text file ..
meat
pork
fish
 
Showing remaining lines that 'not saved a file' ..
beef
beef2

Code:
## justdoit split ## 
#!/bin/bash
i=0;f=0;l=$2;file=$1
c=$(echo $(sed -n '$=' $1) /3|bc)
while [ $(( c -=1 )) -gt -1 ] ; do
((i++));sed -n "$(($f+1)),$(($f+$l)) p" $file >$file${i};f=$(($f++$l));
done
while [ $(( x +=1 )) -le $i ] ; do
echo -e "Showing 'numbered '$file${x}' text file .. \n$(more $file${x})\n"
done
echo -e "Showing remaining lines that 'not saved a file' .. \n$(sed -n "$(($f+1)),$ p" $file)\n"

or try like this if you has split (files starts index 0.i.e..file0 , file2..)
Code:
# split -l3 -d -a1 file file

regards
ygemici
# 5  
Old 06-18-2011
Hi.

There is a perl code that performs like csplit (q.v.), but with alternation allowed (because it's a perl code). So to split a file into sections, one could do something like this, (assuming that the code is downloaded to a local file ppt-split). Here is the driver code, see the URL if you are interested in this or other perl work-alikes:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate perl version of split with regular expressions.

split=./ppt-split
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C $split
v1="cpansearch.perl.org/src/CWEST/ppt-0.14/html/commands/split/split.lafferty"
pe "http://${v1}"

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

rm -f x*
pl " Results, single heading, number:"
$split -p 'string' $FILE
ls -lgG x*

rm -f x*
pl " Results, multi-heading, number|string:"
$split -p 'number|string' $FILE
ls -lgG x*

pl " Sample of one of the files, x.aac:"
cat x.aac

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
ppt-split - ( local: RepRev 1.2, ./ppt-split, 2011-06-18 )
http://cpansearch.perl.org/src/CWEST/ppt-0.14/html/commands/split/split.lafferty

-----
 Input data file data1:
string
able
baker
charlie
number
1
2
3
string
dog
easy
fox
number
4
5
6

-----
 Results, single heading, number:
-rw-r--r-- 1  0 Jun 18 15:14 x.aaa
-rw-r--r-- 1 39 Jun 18 15:14 x.aab
-rw-r--r-- 1 33 Jun 18 15:14 x.aac

-----
 Results, multi-heading, number|string:
-rw-r--r-- 1  0 Jun 18 15:14 x.aaa
-rw-r--r-- 1 26 Jun 18 15:14 x.aab
-rw-r--r-- 1 13 Jun 18 15:14 x.aac
-rw-r--r-- 1 20 Jun 18 15:14 x.aad
-rw-r--r-- 1 13 Jun 18 15:14 x.aae

-----
 Sample of one of the files, x.aac:
number
1
2
3

This kind of thing -- acquiring and downloading utilities that are usually more general than single-line solutions -- is not for everyone, but it is a useful technique that can be used to add to one's personal / private / professional toolset ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split the File based on Size

I have a file that is about 7 GB in size. The requirement is I should split the file equally in such a way that the size of the split files is less than 2Gb. If the file is less than 2gb, than nothing needs to be done. ( need to done using shell script) Thanks, (4 Replies)
Discussion started by: rudoraj
4 Replies

2. UNIX for Advanced & Expert Users

Split one file to many based on pattern

Hello All, I have records in a file in a pattern A,B,B,B,B,K,A,B,B,K Is there any command or simple logic I can pull out records into multiple files based on A record? I want output as File1: A,B,B,B,B,K File2: A,B,B,K (9 Replies)
Discussion started by: deal1dealer
9 Replies

3. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

4. Shell Programming and Scripting

How to Split File based on String?

hi , The scenario is like this, i have a large text files (max 5MB , about 5000 file per day ), Inside almost each line of this file there is a tag 3100.2.22.1 (represent Call_Type) , i need to generate many filess , each one with distinct (3100.2.22.1 Call_Type ) , and one more file to... (3 Replies)
Discussion started by: OTNA
3 Replies

5. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

6. UNIX for Dummies Questions & Answers

Split file based on column

i have file1.txt asdas|csada|130310|0423|A1|canberra sdasd|sfdsf|130426|2328|A1|sydney Expected output : on eaceh third and fourth colum, split into each two characters asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney (10 Replies)
Discussion started by: radius
10 Replies

7. Shell Programming and Scripting

Split the file based on pattern

Hi , I have huge files around 400 mb, which has clob data and have diffeent scenarios: I am trying to pass scenario number as parameter and and get required modified file based on the scenario number and criteria. Scenario 1: file name : scenario_1.txt ... (2 Replies)
Discussion started by: sol_nov
2 Replies

8. Shell Programming and Scripting

Split the file based on column

Hi, I have a file sample_1.txt (300k rows) which has data like below: * Also each record is around 64k bytes 11|1|abc|102553|125589|64k bytes of data 10|2|def|123452|123356|...... 13|2|geh|144351|121123|... 25|4|fgh|165250|118890|.. 14|1|abc|186149|116657|......... (6 Replies)
Discussion started by: sol_nov
6 Replies

9. Shell Programming and Scripting

Split the file based on date value

Hi frnds, I have flat file as . Say : output-file1.txt Output-file2.txt (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

10. 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
Login or Register to Ask a Question