Break a large file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break a large file
# 1  
Old 03-08-2013
Break a large file

Hi Friends,

I have the following file and I would like to split it after every line that starts with
Code:
done

The file is like this

Code:
cat script

#!/bin/bash
#
# Name: name
# Task: name
#
#$ -N name
#$ -S /bin/bash
#$ -m be
#$ -M xxx
#$ -e xxx
#$ -o xxx

cd /path

#!/bin/bash

while read line
do
  	c=$(program ${line} | wc -l )
        line=${line/:/ }
        line=${line/-/ }
        echo "${line} ${c}"
done < input > output

#!/bin/bash
#
# Name: name
# Task: name
#
#$ -N name
#$ -S /bin/bash
#$ -m be
#$ -M xxx
#$ -e xxx
#$ -o xxx

cd /path

#!/bin/bash

while read line
do
  	c=$(program ${line} | wc -l )
        line=${line/:/ }
        line=${line/-/ }
        echo "${line} ${c}"
done < input1 > output1

After each done, there can be a blank line or an immediate line starting after it. Whatever it is, I would like to split the file based on the done criteria.So, from the above input, I get two output files.

Code:
cat script1

#!/bin/bash
#
# Name: name
# Task: name
#
#$ -N name
#$ -S /bin/bash
#$ -m be
#$ -M xxx
#$ -e xxx
#$ -o xxx

cd /path

#!/bin/bash

while read line
do
  	c=$(program ${line} | wc -l )
        line=${line/:/ }
        line=${line/-/ }
        echo "${line} ${c}"
done < input > output

Code:
cat script2

#!/bin/bash
#
# Name: name
# Task: name
#
#$ -N name
#$ -S /bin/bash
#$ -m be
#$ -M xxx
#$ -e xxx
#$ -o xxx

cd /path

#!/bin/bash

while read line
do
  	c=$(program ${line} | wc -l )
        line=${line/:/ }
        line=${line/-/ }
        echo "${line} ${c}"
done < input1 > output1

So, far I tried this

Code:
awk '/done/{x="F"++i;next}{print > x;}' script

But, I see an error saying

Code:
awk: null file name in print or getline
 input record number 1, file om
 source line number 1

# 2  
Old 03-08-2013
Code:
awk ' BEGIN {
                i = 1
                F = "script" i
} /^done/ {
                print $0 > F
                close(F)
                i += 1
                F = "script" i
                next
} {
                print $0 > F
} ' script

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-08-2013
Code:
awk '{print >f}/done/{close(f);f="script" ++n}' f='script1' n=1 myFile

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 03-09-2013
Hi.

Utility csplit was designed for situations like this:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate context splitting with csplit.

# 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 csplit

FILE=${1-data1}

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

# Remove debris from previous runs.
rm -f xx*
pl " Results, split files:"
csplit -z -s $FILE '/^done/+1' {*}
head xx*

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) 
bash GNU bash 3.2.39
csplit (GNU coreutils) 6.10

-----
 Input data file data1:
stuff1
done other tokens1
stuff2
done other tokens2

-----
 Results, split files:
==> xx00 <==
stuff1
done other tokens1

==> xx01 <==
stuff2
done other tokens2

See man csplit for details.

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

Break output file into three files

Help! :) I am getting an output file that looks similar to below. EMAIL_ADDR ----------------------------------------------------------------------------------- user@gmail.com DATABASENAME ----------------------------------------------------------------------------------- db1 db2 db3... (6 Replies)
Discussion started by: cpolikowsky
6 Replies

2. Shell Programming and Scripting

Break Column nth in a CSV file into two

Hi Guys, Need help with logic to break Column nth in a CSV file into two for e.g Refer below the second column as the nth column "abcd","","type/beta-version" need output in a following format "abcd","/place/asia/india/mumbai","/product/sw/tomcat","type/beta-version" ... (5 Replies)
Discussion started by: awk-admirer
5 Replies

3. Shell Programming and Scripting

Line Break Issue in XML file

Hi Experts, Kindly help me to resole the line break issue mentioned below sample xml file: sample source File: ============== <?!New XML File><NEWRECORDS xmlns... (3 Replies)
Discussion started by: UnniVKN
3 Replies

4. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

5. Shell Programming and Scripting

Page Break in a file for printing

Hi, We have 1lac records in source file and unix script will genarate around 1000 files. From target location the files are taking for printing on physical papers. the page size limitation : 256 Lines Can you please tell me how to insert the page break in a flat file for printer. (5 Replies)
Discussion started by: koti_rama
5 Replies

6. Shell Programming and Scripting

Page Break in large file

Hi, The shell script inserting the millions of rows into target flat file system and handling the line number for each line. We need a page break line after every 10,000 lines. is there any command to insert a page break line into target file. (3 Replies)
Discussion started by: koti_rama
3 Replies

7. Shell Programming and Scripting

How to remove line break character in a file

Hi, we are trying to process a csv file,in which we are getting data with line breaks.How to remove the line break character in the file? when i try to print the line break charcter using od -c,it gives as '\n' character for both line break and line feed. Please provide your valuable... (6 Replies)
Discussion started by: cnraja
6 Replies

8. Shell Programming and Scripting

Perl break a file

I am trying to break a large file into smaller ones, with the break defined by the character "*". The following is the code I have so far which breaks the input file on every line instead of at the "*" character. Input file: 1 2 3 4 *5 6 7 8 9 Code: #!/usr/bin/perl... (2 Replies)
Discussion started by: cold_Que
2 Replies

9. UNIX for Dummies Questions & Answers

to break a file into 2 files after matching a pattern.

Hi, i need to break a file into 2 files afetr matching a pattern for ex. there is a fil, file .txt which contains here i need to look for mat $ demon if it matches then i need to transfer the data into another file till the line in which a "d6s" comes,and i have to delete tat line... (3 Replies)
Discussion started by: manit
3 Replies

10. Shell Programming and Scripting

Break a file into separate files

Hello I am facing a scenario where I have a file with XML content and I am running shell script over it. But the problem is the XML is getting updated with new services. In the below scenario, my script takes values from the xml file from one service name say ABCD. Since there are multiple, it is... (8 Replies)
Discussion started by: chiru_h
8 Replies
Login or Register to Ask a Question