complex requirement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting complex requirement
# 1  
Old 08-18-2011
complex requirement

i have a requirement to search a pattern1 and once the pattern1 is found i have to go up and search pattern2 and if pattern2 is found i have to search down for pattern3 and pattern4. once this cycle is compelted, again i have to search pattern1 and pattern2,3,4

i am able to do this by opening the file in vi mode. but pattern1 will be repeating many times and i want to create this as a shell script. as far i am aware i can move the search pointer up and down in vi mode but not using grep or sed or awk commands.

is there any way to run the vi commands in shell script?

sample text file
Code:
pattern2
xxxxxxxxxx
pattern3
xxxxxxx
yyyyyyyyyyy
pattern1
zzzzzzzz
zzzzzzzzz
pattern4
cccccccccccc
vvvvvvvvvvvvv
pattern2
iiiiiiiiiiiiiiiiiiiiiiiiii
pattern3
ggggggggggggggggg
hhhhhhhhhhhhhhhhhh
pattern1
llllllllllllllllllllllllll
kkkkkkkkkkkkkk
pattern4

i also want to repeat the cycle until pattern1 is found.

can any one help me on this?

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 08-18-2011 at 10:26 AM.. Reason: code tags
# 2  
Old 08-18-2011
So whats the output expected from the above sample file..? Can you post us..?
# 3  
Old 08-19-2011
According to this requirement pattern1, pattern2, pattern3 & pattern 4 are fixed values.

pattern3 is the first key for searching. only if pattern3 is found i will search for rest of the patterns.

so on searching if i found first occurence of pattern3 then i will search up for pattern1 and then down for pattern2 and i want to fetch the word next to pattern2 and write it to new file and again search for pattern4.

For better understanding the scenario is like this, pattern1 and pattern4 will indicate the start and ending of a job. the word next to pattern 2 is the name of the job. and pattern3 is key word for example say any database name.

i will have a file that will contain many jobs and i want to find in which jobs i use that database. thats why first i am searching for database name(pattern3) and if it is found i am travelling up to find the starting code for that job using pattern1 and then immediate down occurence for pattern2. (many pattern2 will be found. i want the the pattern2 below pattern1). and once pattern2 is found i will capture the next word (i.e) job name and go down and search for end of that job code(pattern 4).

then again i will go and search for pattern3 if it is present in anyother job.

like this i have to search in that file.
# 4  
Old 08-19-2011
Check if the below one works.. if not you would need to post the few lines from actual file.
Code:
 sed -n 'H;/pattern4/{x;/pattern3/s/.*pattern1[^\n]*\n[^n][^2]*pattern2 \([^\n]*\).*/\1/p}' inputfile

# 5  
Old 08-23-2011
hi Michael,


Thanks for your help. The command is executing. but where should i check the output.

The sample lines in the file to be searched is

Code:
 
BEGIN TEST
   NAME "A0001_ext_PBIG_Override"
   DateModified "2010-09-23"
   TimeModified "09.15.32"
BEGIN LOOP
      NAME "V0"
      PrimaryType "CContainerView"
      Name "Job"
OR
B.DB_NAME='TEST_DB'
END LOOP
END TEST
BEGIN TEST
   NAME "B0001_ext_Dump"
   DateModified "2010-09-24"
   TimeModified "09.15.32"
BEGIN LOOP
      NAME "V0"
      PrimaryType "CContainerView"
      Name "Job"
OR
c.TEST_NAME='VALUE'
END LOOP
END TEST

in the above sample i want to search for the word "TEST_DB" (i.e pattern3)
once pattern3 is found i have to go above and search the word "BEGIN TEST" (i.e pattern1) and if pattern1 is found then i have to search NAME (i.e pattern2) which comes immediately after pattern1. if pattern2 is found then i want to capture the job name next to pattern2 i.e "A0001_New_Dump" in another file and finally go and search the last word "END TEST" (i.e pattern4). searching for pattern4 will bring the search complete in one job.

after END_TEST then next job will start with BEGIN_TEST. so like this i have to find in which jobs the pattern3 is used.
# 6  
Old 08-23-2011
If you simply run the command in the terminal, output will be displayed in the terminal itself. If you want to re-direct the output to a file then try below..
Code:
sed -n 'H;/END TEST/{x;/TEST_DB/s/.*BEGIN TEST\n *NAME \([^\n]*\).*/\1/p}' inputfile > outfile

# 7  
Old 08-24-2011
Hi Michael,

Thanks a lot for your help. The command is working fine. I would like to know how you framed this command. so that it will be better understanding for me and in case if any enhancement is required i can try with this base..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

VIOS requirement for FC and ethernet

Hello, I have some unclear about how VIOS uses its resources. Assume that VIOS lpar has only 1 physical FC, and we use that to connect to the Lun. Then we install VIOS for the lpar on that Lun. So my question is, afterward, we can still use that physical FC for the virtual FC we create... (3 Replies)
Discussion started by: Phat
3 Replies

2. UNIX for Beginners Questions & Answers

UNIX requirement

Hi Everyone, Can anyone of you help me on how to get files creation time on ftp server/Remote server in unix. Regards, Avinash. (6 Replies)
Discussion started by: Avinash varma
6 Replies

3. UNIX for Dummies Questions & Answers

Script requirement

Please help in creating script for below requirement. I will be running 1 command and will get below entries in a text file say file44.txt ******************************* DFDL1005 06:30 00:05 ABFL2003 10/22 01:10 CFTL1256 10/24 00:10 10/25 09:20 PM ******************************** .... (3 Replies)
Discussion started by: Vinay_3308
3 Replies

4. Shell Programming and Scripting

UNIX - requirement

Hi All, I have a source file with data Name ~ Groups Muni~abc,was,USA_ax,123 Chaitanya~USA_12,was Balaji~123,xyz,was Ramu~123,xyz From the second column i want to extract only the groups that matches the pattern 'USA_%' or if the group = 'was', and ignore any other columns. ... (8 Replies)
Discussion started by: morbid_angel
8 Replies

5. Shell Programming and Scripting

Looping requirement

Hi all, I have little working knowledge in unix shell scripting. I have a requirement where i need to pull out some data in between the strings in the file. Input: TEST a a c f d TEST f e g g TEST Output: (7 Replies)
Discussion started by: satyasrin82
7 Replies

6. Shell Programming and Scripting

URGENT REQUIREMENT

1.Write an automated shell program(s) that can create, monitor the log files and report the issues for matching pattern. (i) Conditions for creating log files. Log file is created with date (example 2010_03_27.log). If the log file size is 10 Mb for a particular day then automatically the log... (3 Replies)
Discussion started by: praveen12
3 Replies

7. Shell Programming and Scripting

Requirement

I am trying to script and came up with a conclusion that I need a do while loop in my statement. I am stuck with the do while syntax. I need to use it alongwith the if then else statement. Can I use it is a big question? I actually need to get all the files that are there from within run_dt to... (1 Reply)
Discussion started by: aronmelon
1 Replies

8. AIX

SPOT requirement

Hey May be a dumb question Can I use a SPOT which is at 5.3 TL6 to boot an LPAR (with 5.3 TL8) in to maintenance mode? Will it work ? Is it mandatory that SPOT should be of same or higher version in such case? Bala (1 Reply)
Discussion started by: balaji_prk
1 Replies
Login or Register to Ask a Question

Featured Tech Videos