Sponsored Content
Top Forums Shell Programming and Scripting Split file by column value, each with header Post 302985745 by Aia on Monday 14th of November 2016 07:49:13 PM
Old 11-14-2016
Can you do Perl?

This one has the advantage of taking in consideration the year of the data.

Save as process_input.pl
Run as perl process_input.pl testcase senhia83.input
Make sure you enter two arguments:
The suffix to the filename
The input filename

Code:
#!/usr/bin/perl
use strict;
use warnings;

my %data;
my $file_concat = shift;

my $header = <>;
while(<>) {
    chomp;
    my @fields = split ',';
    push @{$data{$fields[-1]}{$fields[0]}}, $_;
}

for my $fname (keys %data) {
    for my $year (keys $data{$fname}) {
        open my $fw, '>', "$fname${year}__$file_concat.csv" || die;
        print $fw $header;
        for my $value (@{$data{$fname}{$year}}) {
            print $fw "$value\n";
        }
        close $fw;
    }
}

This User Gave Thanks to Aia For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split large file and add header and footer to each file

I have one large file, after every 200 line i have to split the file and the add header and footer to each small file? It is possible to add different header and footer to each file? (1 Reply)
Discussion started by: ashish4422
1 Replies

2. Shell Programming and Scripting

Split large file and add header and footer to each small files

I have one large file, after every 200 line i have to split the file and the add header and footer to each small file? It is possible to add different header and footer to each file? (7 Replies)
Discussion started by: ashish4422
7 Replies

3. UNIX for Dummies Questions & Answers

split header row into one column

So, I have a massive file with thousands of columns I want a list of the headers in one column in another file. So I need to strip off the top line (can use head-1) But how can I convert from this format: A B C D E F G to A B C D E F G (6 Replies)
Discussion started by: polly_falconer
6 Replies

4. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

5. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

6. UNIX for Dummies Questions & Answers

Column Header in the Spool file

Hi All, I have a problem with the column heading while spooling the data from the database. Since i want the column header, therefore i am not using SET HEADING OFF, and i am getting the header, but for the longer column name, some of the character from the column name is missing. for... (7 Replies)
Discussion started by: Pramod_009
7 Replies

7. Shell Programming and Scripting

Split and add header and trailer from input file

I need to split the file based on pattern from position 34-37 while retaining the header and trailer records in each individual split file Also is it possible to output the TOM and PAT records in the same output file ? I need the output file names same as xyz_pattern_Datetimestamp.txt ... (23 Replies)
Discussion started by: techedipro
23 Replies

8. Shell Programming and Scripting

Sort and Split file with header and custom name

Hi, I am using SUN SOLARIS (SunOS sun4v sparc SUNW, T5240). I have a huge data file with header and trailer. This file gets used into an ETL process. ETL skips the header record (which is the first record of the file) and loads the rest of the record. The file can be delimited (comma,... (5 Replies)
Discussion started by: Saanvi1
5 Replies

9. Shell Programming and Scripting

Split a file based on encountering header

I need to split a file based on headers found Input file file1 content: ADD john mickey DROP matt sam output of file F1 john mickey output of file F2 matt sam (5 Replies)
Discussion started by: Diddy
5 Replies

10. Shell Programming and Scripting

Split large xml into mutiple files and with header and footer in file

Split large xml into mutiple files and with header and footer in file tried below it splits unevenly and also i need help in adding header and footer command : csplit -s -k -f my_XML_split.xml extrfile.xml "/<Document>/" {1} sample xml <?xml version="1.0" encoding="UTF-8"?><Recipient>... (36 Replies)
Discussion started by: karthik
36 Replies
GETLASTLOGX(3)						   BSD Library Functions Manual 					    GETLASTLOGX(3)

NAME
getlastlogx, getutmp, getutmpx, updlastlogx, updwtmpx, utmpxname -- user accounting database functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <utmpx.h> struct lastlogx * getlastlogx(const char *fname, uid_t uid, struct lastlogx *ll); void getutmp(const struct utmpx *ux, struct utmp *u); void getutmpx(const struct utmp *u, struct utmpx *ux); int updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll); int updwtmpx(const char *file, const struct utmpx *utx); int utmpxname(const char *fname); DESCRIPTION
The getlastlogx() function looks up the entry for the user with user id uid in the lastlogx(5) file given by fname and returns it in ll. If the provided ll is NULL, the necessary space will be allocated by getlastlogx() and should be free()d by the caller. The getutmp() function fills out the entries in the struct utmp u with the data provided in the struct utmpx ux. getutmpx() does the oppo- site, filling out the entries in the struct utmpx ux with the data provided in the struct utmp u, and initializing all the unknown fields to 0. The sole exception is the ut_type field, which will be initialized to USER_PROCESS. The updlastlogx() function tries to update the information for the user with the user id uid in the lastlogx(5) file given by fname with the data supplied in ll. A struct lastlogx is defined like this: struct lastlogx { struct timeval ll_tv; /* time entry was created */ char ll_line[_UTX_LINESIZE]; /* tty name */ char ll_host[_UTX_HOSTSIZE]; /* host name */ struct sockaddr_storage ll_ss; /* address where entry was made from */ }; All the fields should be filled out by the caller. The updwtmpx() function updates the wtmpx(5) file file with the utmpx(5) entry utx. The utmpxname() function sets the default utmpx(5) database file name to fname. RETURN VALUES
getlastlogx() returns the found entry on success, or NULL if it could not open the database, could not find an entry matching uid in there, or could not allocate the necessary space (in case ll was NULL). utmpxname() returns 1 on success, or 0 if the supplied file name was too long or did not end with 'x'. updlastlogx() and updwtmpx() return 0 on success, or -1 in case the database or file respectively could not be opened or the data not written into it. SEE ALSO
endutxent(3), loginx(3), utmpx(5) HISTORY
The functions getutmp(), getutmpx(), updwtmpx(), and utmpxname() first appeared in Solaris. getlastlogx and updlastlogx first appeared in NetBSD 2.0. BSD
August 26, 2003 BSD
All times are GMT -4. The time now is 12:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy