The UNIX and Linux Forums  
Hello and Welcome from to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[help]Delete or replace text in multiple file and multiple directory dzufauzan Shell Programming and Scripting 16 06-05-2009 12:07 PM
Delete repeated word in text file vincyoxy Shell Programming and Scripting 6 03-26-2009 04:10 AM
c program to extract text between two delimiters from some text file kukretiabhi13 High Level Programming 7 12-03-2008 06:29 PM
How to extract data from a text file negixx Shell Programming and Scripting 1 07-19-2005 10:30 PM
Huge (repeated Entry) text files axl SUN Solaris 4 07-16-2004 08:05 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-13-2009
apjneeraj apjneeraj is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 14
Extract multiple repeated data from a text file

Hi,

I need to extract data from a text file in which data has a pattern. I need to extract all repeated pattern and then save it to different files.
example:

input is:
Code:
ST*867*000352214
BPT*00*1000352214*090311
SE*1*1

ST*867*000352215
BPT*00*1000352214*090311
SE*1*2

ST*867*000352216
BPT*00*1000352214*090311
SE*1*3
output is like:

first file:
Code:
ST*867*000352214
BPT*00*1000352214*090311
SE*1*1
second file:
Code:
ST*867*000352215
BPT*00*1000352214*090311
SE*1*2
Third File:
Code:
ST*867*000352216
BPT*00*1000352214*090311
SE*1*3
You see , output is splitted into three different files based on ST-SE segment repeated in the file.

Can anybody please help me to put me close to solution?

Thanks in advance.

Regards,
Neeraj

Last edited by Yogesh Sawant; 04-13-2009 at 08:10 AM.. Reason: added code tags
  #2 (permalink)  
Old 04-13-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
Try this:

Code:
awk '/^ST/{++i}{print > "file" i}' textfile
Regards
  #3 (permalink)  
Old 04-14-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
seems perl is a good choice to handle it.

Code:
$/="\n\n";
open $fh,"<","a.txt";
while(<$fh>){
	my $tmp=sprintf("%s.txt",$.);
	open my $fh1,">",$tmp;
	print $fh1 $_;
	close $fh1;
}
  #4 (permalink)  
Old 04-14-2009
Whiteboard's Avatar
Whiteboard Whiteboard is offline
Registered User
  
 

Join Date: Feb 2009
Location: @........
Posts: 111
Quote:
Originally Posted by Franklin52 View Post
Try this:

Code:
awk '/^ST/{++i}{print > "file" i}' textfile
Regards
Please explain your code

Thanks
white
  #5 (permalink)  
Old 04-14-2009
apjneeraj apjneeraj is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 14
Thanks all of you.I used the above code and it worked perfectly.

Thanks once again.

Regards
Neeraj
  #6 (permalink)  
Old 04-14-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
Quote:
Originally Posted by Whiteboard View Post
Please explain your code

Thanks
white
Code:
awk '/^ST/{++i}{print > "file" i}' textfile
Code:
/^ST/{++i}
If the line begins with ST increase the variable i. The first time the name of the output file is file1, the next time the line begins with ST the name of the output file becomes file2 and so forth.

Code:
{print > "file" i}
Print the line to the file "file"i.

Regards
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0