Split file - Include first and last row in each file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split file - Include first and last row in each file.
# 1  
Old 05-08-2009
Split file - Include first and last row in each file.

Hi All,

We have file structure as following. (Coma ';' delimiter file)

Header
1;...
1;...
1;...
2;...
2;...
2;...
Trailer

We need to split this file as following. (for each change in first field, after header, we need to create new file with Header and Trailer line)

Header
1;...
1;...
1;...
Trailer

Header
2;...
2;...
2;...
Trailer

Please advice how we can do this.

Thanks.
# 2  
Old 05-08-2009
Hi the below code will create the separate files for each diffrent first value.

Code:
TESTING>cat awk_input1.txt
Header
1;...
1;...
1;...
2;...
2;...
2;...
Trailer

awk -F";" '{ print >> "file"_$1}' awk_input1.txt

Try to modify the above code to place the header and footer in each file.

Smilie Smilie
# 3  
Old 05-08-2009
Thanks panyam. I will try this code.
# 4  
Old 05-08-2009
After the above command .you can do something like this

TEST> for i in `ls file*`
> do
> echo "Header" > ${i}"_changed"
> cat $i >> ${i}"_changed"
> echo "Trailer" >> ${i}"_changed"
> done

my code also generates a file for Header and Footer Smilie ..Don't forget to get rid of that or delete the files at the end.
# 5  
Old 05-08-2009
Quote:
Originally Posted by panyam
After the above command .you can do something like this
Code:
TEST>  for  i in `ls file*`

for i in file*

Last edited by vgersh99; 05-08-2009 at 10:50 AM.. Reason: missing tags
# 6  
Old 05-11-2009
Hi All,

Thanks for your help. I am using following code and its working fine.

awk -F";" '{ print >> "file"_$1}' Testfile.txt
for i in `ls file*`
do
awk '/^HEADER/ {print $0 }' Testfile.txt > ${i}"_CHANGED"
cat $i >> ${i}"_CHANGED"
awk '/^TRAILER/ {print $0 }' Testfile.txt >> ${i}"_CHANGED"
echo "\n" >> ${i}"_CHANGED"
done
rm *DFHDR* *DFTLR*

Thanks.
# 7  
Old 05-18-2009
Hi
This code works for small files.
but for large files, its giving error...

awk: too many output files 10
record number 222

its creating max 10 files.... any option that we need to change so that code creates more files.

Please advice.
thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

3. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

4. Shell Programming and Scripting

Split a file by start and end row.

I have a file which looks something as following, I would like to split to several files, The start and end of each file is 'FILE' and end with 'ASCII... ' . At the same time for each file in the first column add 100 and also second column add 100 the rest of the column as it is , see example of... (2 Replies)
Discussion started by: tk2000
2 Replies

5. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

6. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

7. Shell Programming and Scripting

split row into lines and insert file name

I have a directory with several hundred files. The file format is a space delimited row with an unknown number of columns: A B C D E F G ... I need to turn this format File1 A File1 B File2 A File3 A File3 B File3 C ... I can use grep to display the filename next to each row of... (2 Replies)
Discussion started by: newreverie
2 Replies

8. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

9. Shell Programming and Scripting

Split a record ( in a row) and make it as new row

Hi All, The following is the scenario id name -------------- 1 William;Johnson 2 Azim;Abdul 3 Grasim . . etc.... I need the following output id name -------------- 1 William 1 Johnson 2 Azim (2 Replies)
Discussion started by: kottursamy
2 Replies

10. Programming

Not able to compile Pro*c file due - give errors and points to /usr/include/.. file

I am trying to compile the pro*C file but gives errors. It says it encountered "std" while it was expecting ; , = ( $ $ORACLE_HOME/bin/proc tradewind/dataaccess/Blob.pcc Pro*C/C++: Release 10.2.0.3.0 - Production on Fri May 9 11:10:54 2008 Copyright (c) 1982, 2005, Oracle. All rights... (0 Replies)
Discussion started by: shafi2all
0 Replies
Login or Register to Ask a Question