Sponsored Content
Top Forums Shell Programming and Scripting Split a file using 2-D indexing system Post 302775461 by Don Cragun on Tuesday 5th of March 2013 12:38:40 AM
Old 03-05-2013
If the awk on your system only supports single character settings for RS, or if you'd like to base the output filenames on the input filenames, be able to specify more than one input file, and be able to specify the number of files to be produced before updating the value of the 1st numeric value in the output filename, you could try the following script:
Code:
#!/bin/ksh
cnt=3
Usage="Usage: $(basename $0) [-n cnt] file..."
# Split input file(s) into files named file.X.Y where X and Y reset to 1
# and 1, respectively, for each file operand.  A new file is created
# when a line in an input file starts with a <greater-than> character
# (">").  Lines starting with a <greater-than> character are not
# included in any of the output files, but all other lines are copied 
# unchanged into the corresponding output file.  When a new file is
# created, Y is incremented until it exceeds cnt (which defaults to 3 if
# the -n option is not given on the command line.  When Y exceeds cnt, X
# is incremented and Y is reset to 1.
while getopts n: opt
do      case $opt in
        (n)     cnt="$OPTARG";;
        (?)     echo "$Usage" >&2
                exit 1
        esac
done
shift $(($OPTIND - 1))
if [ $# -lt 1 ]
then    echo "$(basename $0): At least one file operand is required." >&2
        echo "$Usage" >&2
        exit 2
fi
awk -v cnt=$cnt '
FNR == 1 {
        # This is the first record of a new input file.
        # If this is not the first input file, close the last output file for
        # the previous input file.
        if(NR != FNR) close(fn)
        # Create output filename based on input filename.
        x = y = 1
        fn = FILENAME "." x "." y
}
/^>/ {  # Close current output file
        close(fn)
        if(y == cnt) {
                y = 1
                x++
        } else  y++
        fn = FILENAME "." x "." y
        next
}
{       print > fn
}' "$@"

It uses the Korn shell, but will also work with any other shell that accepts parameter expansions specified by the POSIX Standards (including bash).

Note that if the first line in an input file or two or more adjacent lines in an input file start with a >, empty files will not be created; the corresponding filename will just be skipped.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

2. Shell Programming and Scripting

Array indexing in shell

Hi , I have 4 array as below Input: servernames=(10.144.0.129 10.144.0.130 10.144.0.131) subfolder_129=(PSTN_SigtranCamel_03 PSTN_SigtranCamel_04 PSTN_SigtranCamel_05) subfolder_130=(SigtranCamel_11 SigtranCamel_12 SigtranCamel_13 SigtranCamel_14 SigtranCamel_15)... (4 Replies)
Discussion started by: sushmab82
4 Replies

3. Shell Programming and Scripting

[ask]filtering file to indexing...

dear all, i have file with format like this file_master.txt 20110212|231213|rio|apri|23112|222222 20110212|312311|jaka|dino|31223|543234 20110301|343322|alfan|budi|32131|333311 ... i want filter with output like this index_nm.txt rio|apri jaka|dino ... index_years.txt 20110212... (7 Replies)
Discussion started by: zvtral
7 Replies

4. Shell Programming and Scripting

indexing list of words in a file

Hey all, I'm doing a project currently and want to index words in a webpage. So there would be a file with webpage content and a file with list of words, I want an output file with true and false that would show which word exists in the webpage. example: Webpage content data.html ... (2 Replies)
Discussion started by: Johanni
2 Replies

5. Shell Programming and Scripting

indexing a file

hello guys, I have a file like this: input.dat Push-to-talk No Coonection IP support Support for IP telephony Yes Built-in SIP stack Yes Support via software Yes Microsoft Support for Microsoft Exchange Yes UMA (5 Replies)
Discussion started by: Johanni
5 Replies

6. UNIX for Dummies Questions & Answers

awk, array indexing

cat filename|nawk ' { FS="="; if (!a++ == 0) print $0 } ' can anyone plz explain how does array inexing works,how it is evaluating if (!a++ == 0)?? (2 Replies)
Discussion started by: dreamzalive
2 Replies

7. Shell Programming and Scripting

Indexing Variable Names

Hi All I think I might have bitten off more than I can chew here and I'm hoping some of you guys with advanced pattern matching skills can help me. What I want to do is index the occurrence of variable names within a library of scripts that I have. Don't ask why, I'm just sad like that... ... (3 Replies)
Discussion started by: bbq
3 Replies

8. UNIX for Dummies Questions & Answers

Single Liner for indexing

Hello, This is pretty simple, I`m looking for a faster and better method than brute force that I`m doing. I have a 20GB file looks like Name1,Var1,Val1 Name1,Var2,Val2 Name2,Var1,Val3 Name2,Var2,Val4 I want 3 files. Nameindex 1 Name1 2 Name2 ... (2 Replies)
Discussion started by: senhia83
2 Replies

9. Solaris

Split a big file system to several files

Gents Actually I have question and i need your support. I have this NAS file system mounted as /coresys has size of 7 TB I need to Split this file system into several file systems as mount points I mean how to can I Split it professionally to different NAS mount points how to can I decide... (2 Replies)
Discussion started by: AbuAliiiiiiiiii
2 Replies

10. Solaris

Split huge File System

Gents I have huge NAS File System as /sys with size 10 TB and I want to Split each 1TB in spirit File System to be mounted in the server. How to can I do that without changing anything in the source. Please your support. (1 Reply)
Discussion started by: AbuAliiiiiiiiii
1 Replies
UNKNOWN(1)						      General Commands Manual							UNKNOWN(1)

NAME
unknown - identify possible genotypes for unknowns SYNOPSIS
A program to rapidly identify which genotypes are possible for individuals typed as unknowns in the input pedigree. unknown [ -cl ] DESCRIPTION
unknown infers possible genotypes and mating combinations for parents with unknown genotypes for ilink(1), mlink(1) and linkmap(1). OPTIONS
-c Use conditional allele frequencies. -l Choose a good set of loop breakers automatically. RETURN VALUE
0 Successful completion ERRORS
10 File not found 255 Failure EXAMPLES
Normally, unknown(1) is run immediately prior to its sister programs, ilink(1), mlink(1) and linkmap(1), like this: unknown mlink FILES
unknown(1) reads the two files pedfile.dat and datafile.dat as its own input and produces various temporary files that are used as input to the next program. These temporary files are ipedfile.dat, upedfile.dat, speedfile.dat and newspeedfile.dat. NOTES
unknown(1) is part of the FASTLINK package, which is a re-implementation of the LINKAGE suite of computer tools that help investigate genetic linkage as first proposed G.M. Lathrop, J.M. Lalouel, C. Julier, and J. Ott. AUTHORS
Dylan Cooper, Alejandro Schaffer, and Tony Schurtz based on work originally by Jurg Ott, Ph.D, et. al. This manual page was written by Elizabeth Barham <lizzy@soggytrousers.net> for the Debian GNU/Linux system (but may be used by others). WORD-WIDE-WEB http://www.ncbi.nlm.nih.gov/CBBResearch/Schaffer/fastlink.html SEE ALSO
ilink(1), linkmap(1), lodscore(1), mlink(1). April 15, 2003 UNKNOWN(1)
All times are GMT -4. The time now is 03:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy