Sponsored Content
Top Forums Shell Programming and Scripting Assistance with an awk code to split files but keep the header Post 302880593 by colecandoo on Friday 20th of December 2013 09:15:39 PM
Old 12-20-2013
here is a sample of the contents. it is actually about 25-30 fields long, depending on what headers the client had in their database. I've redacted it to four fields for this sample. fields 1 and 2 will always be sort order and sort code.

here is what my awk code does so far:

Code:
start with

workbook.txt
Sort_Order    SortCode    Clientno    Clientname
377    BD 001 0886    L61479908    John Smith    
378    BD 001 0886    L74058317    Jane Smith    
379    BD 002 4518    L61508821    Jane Doe    
380    BD 002 4518    L60998465    John Doe    
381    BD 002 4518    L25019592    John Jones    
382    BD 002 4518    L25143489    Jane Jones    

resulting output

BD 001.txt
377    BD 001 0886    L61479908    John Smith    
378    BD 001 0886    L74058317    Jane Smith    

BD 002.txt
379    BD 002 4518    L61508821    Jane Doe    
380    BD 002 4518    L60998465    John Doe    
381    BD 002 4518    L25019592    John Jones    
382    BD 002 4518    L25143489    Jane Jones

here is what I would like to happen:

Code:
start with

workbook.txt
Sort_Order    SortCode    Clientno    Clientname
377    BD 001 0886    L61479908    John Smith    
378    BD 001 0886    L74058317    Jane Smith    
379    BD 002 4518    L61508821    Jane Doe    
380    BD 002 4518    L60998465    John Doe    
381    BD 002 4518    L25019592    John Jones    
382    BD 002 4518    L25143489    Jane Jones    

desired output

BD 001.txt
Sort_Order    SortCode    Clientno    Clientname
377    BD 001 0886    L61479908    John Smith    
378    BD 001 0886    L74058317    Jane Smith    

BD 002.txt
Sort_Order    SortCode    Clientno    Clientname
379    BD 002 4518    L61508821    Jane Doe    
380    BD 002 4518    L60998465    John Doe    
381    BD 002 4518    L25019592    John Jones    
382    BD 002 4518    L25143489    Jane Jones

please let me know if there is any more information that is required

Colly
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to split in to 2 files

Hi, I have a problem in grepping a file for 2 strings and writing them to 2 appropriate files. I need to use the awk command and read the file only once and write to the appropriate file. My file is very huge in size and it is taking a long time using cat command and grep command. Can anyone... (3 Replies)
Discussion started by: m_subra_mani
3 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. Shell Programming and Scripting

Need assistance with simple shell script to organize files. [Code attached]

I need some help with this shell script for class. All it does is organize your files. It works, but in the log file, it needs to show the new filepaths of the moved files. Heres my log of my output: Starting to organize... movie2.wmv --> movie3.mov --> movie1.mpg --> song1.mp3 --> ... (3 Replies)
Discussion started by: ryandamartini
3 Replies

4. Shell Programming and Scripting

awk split and rename files

I have a file test1.html like below: <dctm_topnav_en_US> <html> ..... </html> <dctm_topnav_en_CA> <html> ..... </html> <dctm_topnav_en_FR> <html> ..... </html> I need to use awk to split this into three file names like en_US.html , en_CA.html, en_FR.html each having content between... (4 Replies)
Discussion started by: vijay52
4 Replies

5. Shell Programming and Scripting

Need Header for all splitted files - awk

Input file: i have a file and need to split into multiple files based on first column. i need the header for all the splitted files. I'm unable to get the header. $ cat log.txt id,mailtype,value 1252468812,yahoo,3.5 1252468812,hotmail,2.4 1252468819,yahoo,1.2 1252468812,msn,8.9... (6 Replies)
Discussion started by: mannefromdetroi
6 Replies

6. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

7. Shell Programming and Scripting

Adding header to sub files after splitting the main file using AWK

Hi Folks, I have a file like: mainfile.txt: ------------- file1 abc def xyz file1 aaa pqr xyz file2 lmn ghi xyz file2 bbb tuv xyz I need output having two files file1 and file2. file1: ------ Name State Country abc def xyz aaa pqr xyz file2: (3 Replies)
Discussion started by: tanmay.gemini
3 Replies

8. Shell Programming and Scripting

Split file by column value, each with header

Hello all, I have a csv with with different testcase values in column 5. year,min,max,Instrument,Testcase 2016,201,1003,GEOTROPH-02116,TATA7980 2016,53,1011,GEOTROPH-01963,TATA7980 2016,3,1024,GEOTROPH-02067,TATA7980 2016,203,1027,GEOTROPH-02011,TATA7980... (16 Replies)
Discussion started by: senhia83
16 Replies

9. UNIX for Beginners Questions & Answers

awk assistance - Comparing 2 csv files

Hello all, I have searched high and low for a solution to this, many have come really close but not quite what I'm after. I have 2 files. One contains GUID's, for example: 8121E002-96FE-4C9C-BC5A-6AFF20DACECD 84468F30-F3B7-418B-81F0-0908E80792BF A second file, contains a path to the... (8 Replies)
Discussion started by: tirmUK
8 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 02:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy