split a file - help required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split a file - help required
# 8  
Old 09-11-2008
Thank you very much radoulov. If you could please explain the code lines would be very much helpful specially the use of _
# 9  
Old 09-12-2008
awk:
Code:
nawk 'BEGIN{
n=1
flag=1
}
{
	if(NR==1)
		a=$0
	if(NR==2)
		b=$0
	if(NR>2)
	{
        	file=sprintf("a%s.txt",n)
        	if(flag)
		{
			print a >> file
			print b >> file
			flag=0
		}
        	print $0 >> file
		if((NR-2)%5==0)
                {
			n++
			flag=1
		}
	}

}' file


perl :
Code:
$flag=1;
$n=1;
open(FH,"<file");
while(<FH>){
	if($.==1){$a=$_;}
	if($.==2){$b=$_;}
	if($.>2)
	{
        	$file=sprintf("a%s.txt",$n);
		open(FH1,">>$file");
        	if($flag)
		{
			print FH1 $a ;
			print FH1 $b ;
			$flag=0;
		}
        	print FH1 $_;
		if(($.-2)%5==0)
                {
			$n++;
			$flag=1;
		}
	}

}
close(FH);


Last edited by summer_cherry; 09-12-2008 at 01:51 AM..
# 10  
Old 09-12-2008
Quote:
Originally Posted by uwork72
Thank you very much radoulov. If you could please explain the code lines would be very much helpful specially the use of _
Sure.

Code:
NR < 3 { hd = hd ? hd RS $0 : $0; next }

Format the header: while reading the first two records: NR < 3, concatenate the first record, a newline and the second record using the ternary operator (if ? then : else). So the hd variable contains the header.

Code:
{ _ = _ ? _ RS $0 : $0; c++ }

While reading the rest of the input (because of the next statement in the previous action), concatenate the records using the same mechanism: there is nothing special in the _ character, it's a regular variable, I use it often as a default variable name (a bit like the Perl special variable _) and increment a counter variable c (I need it because of your requirement for the number of records in the last file).


Code:
!(++i % 5) && c > 2 {
  if (f) close("sub_" f)
  print hd RS _ > ("sub_" ++f)
  _ = c = 0
  }

When the remainder of the division of the incrementing i variable (++i) and 5 is not true (is 0), i.e. every five records,
and (logical) the value of the counter c is greater than 2 (if less than 3 records, continue with the current file), do the following:
close the previous output file (if exists), print to the new one: the header, a newline (RS), and the previously build content (the value of the _ variable). Clean the _ and the c variable at the end (set to 0).

Code:
END { print _ > ("sub_"(c > 2 ? ++f : f)) }

After reading the entire input if there are less than 2 records in the _ variable,
print them in the current file, otherwise, print them in a new one.

Hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Required 3 lines above the file and below file when string matches

i had requirement like i need to get "error" line of above 3 and below 3 from a file .I tried with the below script.But it's not working. y='grep -n -i error /home/file.txt|cut -c1' echo $y head -$y /home/file.txt| tail -3 >tmp.txt tail -$y /home/file.txt head -3 >>tmp.txt (4 Replies)
Discussion started by: bhas85
4 Replies

2. Shell Programming and Scripting

Need help in creating a file in required format form another existing file

I have a text file with contents like this: a,b,c, d~e,f,g,h~i,j ,k,l,m~n,o,p,q~ I need to convert this file into this format unix shell script commands: a,b,c,d~ e,f,g,h~ i,j,k,l,m~ n,o,p,q~ as you may have noticed, I need to retain the ~ signs at the end. Any help is greatly... (3 Replies)
Discussion started by: harsha1238
3 Replies

3. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

4. Shell Programming and Scripting

Help required the cut the whole contents from one file and paste it into new file

Hi, First of all sincere apologies if I have posted in a wrong section ! Please correct me if I am wrong ! I am very new to UNIX scripting. Currently my problem is that I have a code file at the location /home/usr/workarea/GeneratedLogs.log :- Code :- (Feb 7, 571 7:07:29 AM),... (4 Replies)
Discussion started by: acidburn_007
4 Replies

5. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

6. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

7. Shell Programming and Scripting

Only the required tag information the XML file file

Hi i have a single line xml file having many account no tag, from which i need only the account no from the tag. any one can help on this. below is the xml file: ... (2 Replies)
Discussion started by: Saravanapk
2 Replies

8. Shell Programming and Scripting

Getting required fields from a test file in required fromat in unix

My data is something like shown below. date1 date2 aaa bbbb ccccc date3 date4 dddd eeeeeee ffffffffff ggggg hh I want the output like this date1date2 aaa eeeeee I serached in the forum but didn't find the exact matching solution. Please help. (7 Replies)
Discussion started by: rdhanek
7 Replies

9. Shell Programming and Scripting

awk script required for finding records in 1 file with corresponding another file.

Hi, I have a .txt file (uniqfields.txt) with 3 fields separated by " | " (pipe symbol). This file contains unique values with respect to all these 3 fields taken together. There are about 40,000 SORTED records (rows) in this file. Sample records are given below. 1TVAO|OVEPT|VO... (2 Replies)
Discussion started by: RRVARMA
2 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question