awk script issue redirecting to multiple files after matching pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script issue redirecting to multiple files after matching pattern
# 1  
Old 12-11-2014
Wrench awk script issue redirecting to multiple files after matching pattern

Hi All

I am having one awk and sed requirement for the below problem.
I tried multiple options in my sed or awk and right output is not coming out.

Problem Description

###############################################################

I am having a big file say file having repeated patterns . I want to print the text in between these patters along with these patterns in multiple text files means I want to redirect the output to multiple text files.

file

############################################################
Code:
///// BMSNSMS ,.KSKSK
/// BM_WATER//////////
///JSHSHS L;LLLLLLLL
module Template 
input {
}
output { B 
}
port {  se 

}
///// NANAMSNSMS ,.KSKSK
/// BM_WATER//////////
///jajaJSHSHS L;LLLLLLLL
module1 Template1 
input1 {
}
output1 { B 
}
port{  se 

}
port {
C

}
///// KLALALNANAMSNSMS ,.KSKSK
/// BM_WATER//////////
///KAKAjajaJSHSHS L;LLLLLLLL
module2 Template2 
input2 {
}
output2 { CD 
}
port3{  se 

}
port {
D

}
///// AAKLALALNANAMSNSMS ,.KSKSK
/// BM_WATER//////////
///aaKAKAjajaJSHSHS L;LLLLLLLL
module3 Template3
input3 {
}
output3 { CD 
}
port4{  se 

}
port1 {
D

}

###############################################################

So we can see that BM_WATER is a repeated pattern
I want output as shown below

File1
###################################
Code:
///// BMSNSMS ,.KSKSK
/// BM_WATER//////////
///JSHSHS L;LLLLLLLL
module Template 
input {
}
output { B 
}
port {  se 

}


File 2

Code:
///// NANAMSNSMS ,.KSKSK
/// BM_WATER//////////
///jajaJSHSHS L;LLLLLLLL
module1 Template1 
input1 {
}
output1 { B 
}
port{  se 

}
port {
C

}


FILE 3

Code:
///// KLALALNANAMSNSMS ,.KSKSK
/// BM_WATER//////////
///KAKAjajaJSHSHS L;LLLLLLLL
module2 Template2 
input2 {
}
output2 { CD 
}
port3{  se 

}
port {
D

}

FILE 4

Code:
///// AAKLALALNANAMSNSMS ,.KSKSK
/// BM_WATER//////////
///aaKAKAjajaJSHSHS L;LLLLLLLL
module3 Template3
input3 {
}
output3 { CD 
}
port4{  se 

}
port1 {
D

}

I tried this but not working
Code:
awk '/BM_WATER/{x="F"++i;}{print > x;}' file


Last edited by vbe; 12-11-2014 at 10:26 AM.. Reason: extra code tags, but please, use code tags NOT ICODE
# 2  
Old 12-11-2014
Hello kshitij,

Following may help you in same.
Code:
awk '{if($0 ~ /^\/\/\/\/\/.*/){i++;while($0 !~ /^port.*\{/){print $0 >> "File"i".txt";getline}};}' Input_file

Thanks,
R. Singh
# 3  
Old 12-11-2014
Hi Ravinder ,

Thanks for the reply
I tried this but it is not generating output files properly all the port statements are missing in the new output files
Can you help me out with revised script

Thanks
Kshitij
# 4  
Old 12-11-2014
Try
Code:
awk '$0~"^/////"{FN="FILE"++CNT} {print > FN}' file

# 5  
Old 12-11-2014
Hi
Thanks but I need to match it with BM_WATER to split into multiple files.
Thanks
Kshitij
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: matching multiple fields between 2 files

Hi, I have 2 tab-delimited input files as follows. file1.tab: green A apple red B apple file2.tab: apple - A;Z Objective: Return $1 of file1 if, . $1 of file2 matches $3 of file1 and, . any single element (separated by ";") in $3 of file2 is present in $2 of file1 In order to... (3 Replies)
Discussion started by: beca123456
3 Replies

2. Shell Programming and Scripting

Awk: Print Error While Redirecting output in multiple Files

Hi, I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") { array1++ array2++ array3++ } else if (substr($1,26,2)=="03" &&... (4 Replies)
Discussion started by: siramitsharma
4 Replies

3. Shell Programming and Scripting

Creating single pattern for matching multiple files.

Hi friends, I have a some files in a directory. for example 856-abc 856-def 851-abc 945-def 956-abc 852-abc i want to display only those files whose name starts with 856* 945* and 851* using a single pattern. i.e 856-abc 856-def 851-abc 945-def the rest of the two files... (2 Replies)
Discussion started by: Little
2 Replies

4. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

5. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

6. Shell Programming and Scripting

Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern... (3 Replies)
Discussion started by: hnux
3 Replies

7. Shell Programming and Scripting

awk pattern matching and shell issue.

Please help me in this issue. I am unable to get the job,seems the awk not browsing the files. Please find my tries below. I have attached two files : 1.tobesearched.txt - a glimpse of a huge log file. 2.searchstring.txt - searching keys. these are the two scripts i tried writing: ... (7 Replies)
Discussion started by: deboprio
7 Replies

8. UNIX for Dummies Questions & Answers

Script to list non matching files using pattern

Hi, I am trying to write a script that list down all the files that do not match the pattern My pattern will be like this "*.jpg|*.xml|*.sql". This pattern will be stored in a file. The script need to read this pattern from the file and delete the files that does not match this pattern. It... (7 Replies)
Discussion started by: meenavin
7 Replies

9. Shell Programming and Scripting

awk script issue : comparing two files with a pattern

File 1 ################################################################# pma.zcal.iop_pma_zcal_cntl (2710.080 115.200) pma.lanea23.rx0.cntl (696.960 844.800) pma.lanea67.rx0.cntl (1733.760 844.800) pma.zcal.iop_pma_zcal_cust (2280.960 115.200)... (1 Reply)
Discussion started by: jaita
1 Replies

10. UNIX for Dummies Questions & Answers

Matching pattern script (sed or awk?)

Hi Guys, I am new to the forum and to scripting so bear with me. Thanks, Gary. I have 3 files - file1, file2, file3 I am trying to come up with a script that will check the output of these files and if the 1st nine fields are matched in all 3 files, echo "The following string had been... (2 Replies)
Discussion started by: gazza-o
2 Replies
Login or Register to Ask a Question