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.
# 15  
Old 05-18-2009
I agree... but I tried this as well but getting same error. its not taking print >> "file_"$1 command. Any other way to create file ?

nawk -FS=";" '{ print >> "file_"$1}' Testfile.txt
# 16  
Old 05-18-2009
As ghostdog74 mentioned, it's not:

Code:
nawk -FS=";"

but

Code:
nawk -F";"

Have you tried this?

Code:
nawk -F";" '{ print >> "file"_$1; close("file"_$1)}' Testfile.txt

# 17  
Old 05-18-2009
Hi Franklin, yes I tried that and again same error Smilie.

nawk: syntax error at source line 1
context is
{ print >> >>> "file"_ <<< $1; close("file"_$1)}
nawk: illegal statement at source line 1
# 18  
Old 05-18-2009
you forgot to quote the _ as well.....
# 19  
Old 05-18-2009
Quote:
Originally Posted by ghostdog74
you forgot to quote the _ as well.....
Right, it should be:

Code:
nawk -F";" '{ print >> "file_"$1; close("file_"$1)}' Testfile.txt

# 20  
Old 05-18-2009
Hi,

nawk -F";" '{ print >> "file_"$1; close("file_"$1)}' EPA

even this doesnt work.

Error:
nawk: syntax error at source line 1
context is
{ print >> >>> "file_"$1 <<< ; close("file_"$1)}
nawk: illegal statement at source line 1

Actually if I use

awk -F";" '{ print >> "file_"$1; close("file_"$1)}' EPA

then it creates 10 files after that it gives error
awk: too many output files 10
record number 222
# 21  
Old 05-18-2009
Code:
open $fh,"<","a.spl";
while(<$fh>){
	my @tmp=split(";",$_);
	$hash{$tmp[0]}.=$_ if $#tmp>0;
}
for $key (keys %hash){
	my $file=sprintf("file%s.txt",$key);
	open FH,">$file";
	print FH "header\n";
	print FH $hash{$key};
	print FH "footer";
	close FH;
}

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