Sponsored Content
Top Forums Shell Programming and Scripting awk script to split file into multiple files based on many columns Post 302789209 by Yoda on Wednesday 3rd of April 2013 09:41:27 AM
Old 04-03-2013
Quote:
Originally Posted by viored
I'm using a UNIX terminal. The code doesn't do what is requested.
It will be much better if you can show us what exactly you did, what output you got in code tags rather than just simply saying "the code doesn't do what is requested"

Guru's code should work fine, I don't see any issues in it!

But I would also recommend to close the file, because if there are too many files opened, eventually awk may exceed a system limit on the number of open files in one process.

It is best to close each one when the program has finished writing it.
Code:
awk '{F=$2"."$3"."$4".txt";print >> F;close(F)}' inputfile

This User Gave Thanks to Yoda For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk 3 files to one based on multiple columns

Hi all, I have three files, one is a navigation file, one is a depth file and one is a file containing the measured field of gravity. The formats of the files are; navigation file: 2006 320 17 39 0 0 *nav 21.31542 -157.887 2006 320 17 39 10 0 *nav 21.31542 -157.887 2006 320 17 39 20 0... (2 Replies)
Discussion started by: andrealphus
2 Replies

2. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

3. Shell Programming and Scripting

Split a file into multiple files based on the input pattern

I have a file with lines something like. ...... 123_start ...... ....... 123_end .... ..... 456_start ...... ..... 456_end .... ..... 789_start .... .... 789_end (6 Replies)
Discussion started by: abinash
6 Replies

4. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

5. Shell Programming and Scripting

Split a file into multiple files based on field value

Hi, I've one requirement. I have to split one comma delimited file into multiple files based on one of the column values. How can I achieve this Unix Here is the sample data. In this case I have split the files based on date column(c4) Input file c1,c2,c3,c4,c5... (1 Reply)
Discussion started by: manasvi24
1 Replies

6. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

7. Shell Programming and Scripting

Split a big file into multiple files based on first four characters

I have a requirement to split a huge file to smaller text files based on first four characters which look like ABCD 1234 DFGH RREX : : : : : 0000 Each of these records are OF EQUAL bytes with a different internal layout based on the above first digit identifier.. Any help to start... (5 Replies)
Discussion started by: etldev
5 Replies

8. Shell Programming and Scripting

Split a single file into multiple files based on a value.

Hi All, I have the sales_data.csv file in the directory as below. SDDCCR; SOM ; MD6546474777 ;05-JAN-16 ABC ; KIRAN ; CB789 ;04-JAN-16 ABC ; RAMANA; KS566767477747 ;06-JAN-16 ABC ; KAMESH; A33535335 ;04-JAN-16 SDDCCR; DINESH; GD6674474747 ;08-JAN-16... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

9. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

10. UNIX for Beginners Questions & Answers

Split into multiple files by using Unique columns in a UNIX file

I have requirement to split below file (sample.csv) into multiple files by using the unique columns (first 3 are unique columns) sample.csv 123|22|56789|ABCDEF|12AB34|2019-07-10|2019-07-10|443.3400|1|1 123|12|5679|BCDEFG|34CD56|2019-07-10|2019-07-10|896.7200|1|2... (3 Replies)
Discussion started by: RVSP
3 Replies
Dpkg::Compression::FileHandle(3)				   libdpkg-perl 				  Dpkg::Compression::FileHandle(3)

NAME
Dpkg::Compression::FileHandle - object dealing transparently with file compression SYNOPSIS
use Dpkg::Compression::FileHandle; $fh = Dpkg::Compression::FileHandle->new(filename=>"sample.gz"); print $fh "Something "; close $fh; $fh = Dpkg::Compression::FileHandle->new(); open($fh, ">", "sample.bz2"); print $fh "Something "; close $fh; $fh = Dpkg::Compression::FileHandle->new(); $fh->open("sample.xz", "w"); $fh->print("Something "); $fh->close(); $fh = Dpkg::Compression::FileHandle->new(filename=>"sample.gz"); my @lines = <$fh>; close $fh; $fh = Dpkg::Compression::FileHandle->new(); open($fh, "<", "sample.bz2"); my @lines = <$fh>; close $fh; $fh = Dpkg::Compression::FileHandle->new(); $fh->open("sample.xz", "r"); my @lines = $fh->getlines(); $fh->close(); DESCRIPTION
Dpkg::Compression::FileHandle is an object that can be used like any filehandle and that deals transparently with compressed files. By default, the compression scheme is guessed from the filename but you can override this behaviour with the method "set_compression". If you don't open the file explicitly, it will be auto-opened on the first read or write operation based on the filename set at creation time (or later with the "set_filename" method). Once a file has been opened, the filehandle must be closed before being able to open another file. STANDARD FUNCTIONS
The standard functions acting on filehandles should accept a Dpkg::Compression::FileHandle object transparently including "open" (only when using the variant with 3 parameters), "close", "binmode", "eof", "fileno", "getc", "print", "printf", "read", "sysread", "say", "write", "syswrite", "seek", "sysseek", "tell". Note however that "seek" and "sysseek" will only work on uncompressed files as compressed files are really pipes to the compressor programs and you can't seek on a pipe. FileHandle METHODS The object inherits from FileHandle so all methods that work on this object should work for Dpkg::Compression::FileHandle too. There may be exceptions though. PUBLIC METHODS
my $fh = Dpkg::Compression::FileHandle->new(%opts) Creates a new filehandle supporting on-the-fly compression/decompression. Supported options are "filename", "compression", "compression_level" (see respective set_* functions) and "add_comp_ext". If "add_comp_ext" evaluates to true, then the extension corresponding to the selected compression scheme is automatically added to the recorded filename. It's obviously incompatible with automatic detection of the compression method. $fh->ensure_open($mode) Ensure the file is opened in the requested mode ("r" for read and "w" for write). Opens the file with the recorded filename if needed. If the file is already open but not in the requested mode, then it errors out. $fh->set_compression($comp) Defines the compression method used. $comp should one of the methods supported by Dpkg::Compression or "none" or "auto". "none" indicates that the file is uncompressed and "auto" indicates that the method must be guessed based on the filename extension used. $fh->set_compression_level($level) Indicate the desired compression level. It should be a value accepted by the function "compression_is_valid_level" of Dpkg::Compression. $fh->set_filename($name, [$add_comp_ext]) Use $name as filename when the file must be opened/created. If $add_comp_ext is passed, it indicates whether the default extension of the compression method must be automatically added to the filename (or not). my $file = $fh->get_filename() Returns the filename that would be used when the filehandle must be opened (both in read and write mode). This function errors out if "add_comp_ext" is enableactivated while the compression method is set to "auto". The returned filename includes the extension of the compression method if "add_comp_ext" is enabled. $ret = $fh->use_compression() Returns "0" if no compression is used and the compression method used otherwise. If the compression is set to "auto", the value returned depends on the extension of the filename obtained with the get_filename method. my $real_fh = $fh->get_filehandle() Returns the real underlying filehandle. Useful if you want to pass it along in a derived object. DERIVED OBJECTS
If you want to create an object that inherits from Dpkg::Compression::FileHandle you must be aware that the object is a reference to a GLOB that is returned by Symbol::gensym() and as such it's not a HASH. You can store internal data in a hash but you have to use "*$self-"{...}> to access the associated hash like in the example below: sub set_option { my ($self, $value) = @_; *$self->{"option"} = $value; } AUTHOR
Raphael Hertzog <hertzog@debian.org> 1.16.15 2014-06-05 Dpkg::Compression::FileHandle(3)
All times are GMT -4. The time now is 12:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy