Split a file using 2-D indexing system


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a file using 2-D indexing system
# 1  
Old 03-04-2013
Split a file using 2-D indexing system

I have a file and want to split it using a 2-D index system

for example

if the file is p.dat with 6 data sets separated by ">".
I want to set nx=3, ny=2. I need to create files

p.dat.1.1
p.dat.1.2
p.dat.1.3
p.dat.2.1
p.dat.2.2
p.dat.2.3

I have tried using a single index and want to modify it to 2 indices

Code:
awk -v ft=$ftmp '/>/{x=ft"."++i; next} {print > x;}' p.dat

Code:
cat p.dat
  1
  1
  1
>
  2 
  2
  2
>
  3 
  3 
  3
>
  4
  4
  4
>
  5
  5
  5
>
  6
  6
  6


Last edited by kristinu; 03-04-2013 at 11:25 PM..
# 2  
Old 03-05-2013
Try:
Code:
awk -v ft=$ftmp '{i=i++%3+1;  i2=(i==1)?i2+1:i2; print > ft"."i2"."i}' RS="\n>\n"  p.dat


Last edited by mirni; 03-05-2013 at 12:53 PM.. Reason: removed unnecessary OFS="."
This User Gave Thanks to mirni For This Post:
# 3  
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.
# 4  
Old 03-05-2013
I love your second version
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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