Sponsored Content
Top Forums Shell Programming and Scripting How to split file based on subtitle Post 302531928 by drl on Saturday 18th of June 2011 04:23:49 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ARRAY_SHIFT(3)								 1							    ARRAY_SHIFT(3)

array_shift - Shift an element off the beginning of array

SYNOPSIS
mixed array_shift (array &$array) DESCRIPTION
array_shift(3) shifts the first value of the $array off and returns it, shortening the $array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Note This function will reset(3) the array pointer of the input array after use. PARAMETERS
o $array - The input array. RETURN VALUES
Returns the shifted value, or NULL if $array is empty or is not an array. EXAMPLES
Example #1 array_shift(3) example <?php $stack = array("orange", "banana", "apple", "raspberry"); $fruit = array_shift($stack); print_r($stack); ?> The above example will output: Array ( [0] => banana [1] => apple [2] => raspberry ) and orange will be assigned to $fruit. SEE ALSO
array_unshift(3), array_push(3), array_pop(3). PHP Documentation Group ARRAY_SHIFT(3)
All times are GMT -4. The time now is 10:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy