How to print lines till till a pattern is matched in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print lines till till a pattern is matched in loop
# 1  
Old 10-30-2008
How to print lines till till a pattern is matched in loop

Dear All

I have a file like this

112534554
446538656
444695656
225696966
226569744
228787874
113536566
443533535
222564552
115464656
225445345
225533234

I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either 11, 22,44 or 66.

I want the output to be directed to three files named serially like f001, f002, f003.......like this

cat f001
112534554
446538656
444695656
225696966
226569744
228787874

cat f002
113536566
443533535
222564552

cat f003
115464656
225445345
225533234

How can i put this in a loop till the end of read of my input file......

Please help.....

Regards
# 2  
Old 10-30-2008
Code:
awk '/^11/{++c}{print > "f00" c}' file

In case there are too many split files use,

Code:
awk '/^11/{++c;close(f)}{print > (f="f00" c)}' file

# 3  
Old 10-30-2008
Thanks....!!!
The second comand is working perfectly....
# 4  
Old 10-30-2008
awk:

Code:
nawk '/11.*/{n++;f=sprintf("a%s.txt",n);print $0 > f;next}{print $0
>f}' file

perl:

Code:
undef $/;
open FH,"<file" or die "Can not open";
$str=<FH>;
$str=~s/\n11/,11/g;
@arr=split(",",$str);
close FH;
for($i=0;$i<=$#arr;$i++){
	$file=sprintf("f%s.txt",$i+1);
	open FH,">$file";
	print FH $arr[$i];
	close FH;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

2. Shell Programming and Scripting

While loop till length of line is great enough

I have the following code: # Get the line of stations_info.txt starting with "${xstation1} " and copy it to file temp.txt grep "^${xstation1} " stations_info.txt > temp.txt # Get lat and long of station nl=0 ... (2 Replies)
Discussion started by: claire.a
2 Replies

3. Shell Programming and Scripting

For loop till the files downloaded

Need assistance in writing a for loop script or any looping method. Below is the code where i can get all the files from the URL . There are about 80 files in the URL .Every day the files get updated . Script that i wanted is the loop must keep on running till it gets 80 files. It matches the count... (5 Replies)
Discussion started by: ajayram_arya
5 Replies

4. Shell Programming and Scripting

Grep with loop till search is done

I need help to put a script where it runs the svn command grep'ing for the ticket# in the comments to see if the ticket was used in the latest commit. so on command line: ./test.sh ticket-1 ticket-2 ticket-3 It should be able to check if ticket-1 is used first and if not then check if... (2 Replies)
Discussion started by: iaav
2 Replies

5. Shell Programming and Scripting

Pattern match till the end of the file.

I have a file which is like this ……………………………………….. ………………………………… ………………………………… …………………………………… ……………………………………. ……………………………… <<<from_here>>> ……………………………… ………………………………. I want a script which would fetch the data starting from <<<from_here>>> in the file till the end... (2 Replies)
Discussion started by: halfafringe
2 Replies

6. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

Print file till specified line

Hi , I want to print file till line number 33 and stdout to different file. I have used cat -n <file name> which numbers the lines including blank line. now how can i stdout the first 33 lines in to new file. Regards newaix (6 Replies)
Discussion started by: newaix
6 Replies

8. Shell Programming and Scripting

Print characters till the next space when the pattern is found

i have a file which contains alphanumeric data in every line. what i need is the data after certain pattern. the data after the pattern is not of fixed length so i need the data till the space after the pattern. Input file: bfdkasfbdfg khffkf lkdhfhdf pattern (datarequired data not required)... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

9. Shell Programming and Scripting

Need to remove few characters from each line till a pattern is matched

Hi All, I want to remove first few characthers from starting of the line till ',' Comma... which needs to be done for all the lines in the file Eg: File content 1,"1234",emp1,1234 2,"2345",emp2,2345 Expected output is ,"1234",emp1,1234 ,"2345",emp2,2345 How can parse... (4 Replies)
Discussion started by: kiranlalka
4 Replies

10. Shell Programming and Scripting

way to print all the string till we get a space and a number

Is there any way to print all the string till we get a space and a number and store it a variable for eg we have string java.io.IOException: An existing connection was forcibly closed by the remote host 12 All I want is to store "java.io.IOException: An existing connection was forcibly closed... (13 Replies)
Discussion started by: villain41
13 Replies
Login or Register to Ask a Question