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
closefrom(3C)						   Standard C Library Functions 					     closefrom(3C)

NAME
closefrom, fdwalk - close or iterate over open file descriptors SYNOPSIS
#include <stdlib.h> void closefrom(int lowfd); int fdwalk(int (*func)(void *, int), void *cd); DESCRIPTION
The closefrom() function calls close(2) on all open file descriptors greater than or equal to lowfd. The effect of closefrom(lowfd) is the same as the code #include <sys/resource.h> struct rlimit rl; int i; getrlimit(RLIMIT_NOFILE, &rl); for (i = lowfd; i < rl.rlim_max; i++) (void) close(i); except that close() is called only on file descriptors that are actually open, not on every possible file descriptor greater than or equal to lowfd, and close() is also called on any open file descriptors greater than or equal to rl.rlim_max (and lowfd), should any exist. The fdwalk() function first makes a list of all currently open file descriptors. Then for each file descriptor in the list, it calls the user-defined function, func(cd, fd), passing it the pointer to the callback data, cd, and the value of the file descriptor from the list, fd. The list is processed in file descriptor value order, lowest numeric value first. If func() returns a non-zero value, the iteration over the list is terminated and fdwalk() returns the non-zero value returned by func(). Otherwise, fdwalk() returns 0 after having called func() for every file descriptor in the list. The fdwalk() function can be used for fine-grained control over the closing of file descriptors. For example, the closefrom() function can be implemented as: static int close_func(void *lowfdp, int fd) { if (fd >= *(int *)lowfdp) (void) close(fd); return(0); } void closefrom(int lowfd) { (void) fdwalk(close_func, &lowfd); } The fdwalk() function can then be used to count the number of open files in the process. RETURN VALUES
No return value is defined for closefrom(). If close() fails for any of the open file descriptors, the error is ignored and the file descriptors whose close() operation failed might remain open on return from closefrom(). The fdwalk() function returns the return value of the last call to the callback function func(), or 0 if func() is never called (no open files). ERRORS
No errors are defined. The closefrom() and fdwalk() functions do not set errno but errno can be set by close() or by another function called by the callback function, func(). FILES
/proc/self/fd directory (list of open files) USAGE
The act of closing all open file descriptors should be performed only as the first action of a daemon process. Closing file descriptors that are in use elsewhere in the current process normally leads to disastrous results. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
close(2), getrlimit(2), proc(4), attributes(5) SunOS 5.11 27 Apr 2000 closefrom(3C)
All times are GMT -4. The time now is 08:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy